Skip to content

Commit bb890e9

Browse files
authored
Added Indian conversion (and subcontinent) for currencies (#155)
* Indian currency conversion (also for the subcontinent) * updated readme * Unwanted log commented out * Fixes to comments and added info about indian language
1 parent 9664a0a commit bb890e9

File tree

3 files changed

+120
-3
lines changed

3 files changed

+120
-3
lines changed

README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@
1414

1515
## Supported languages
1616

17-
18-
1917
| Code | Flag | Language | Main Region | 42 |
2018
| --------------- | ---- | -------------------------------- | ----------- | ---------------- |
2119
| `en`, `en-us` | 🇺🇸 | American English | USA | forty-two |
2220
| `fr`, `fr-fr` | 🇫🇷 | French, Français | France | quarante-deux |
2321
| `it`, `it-it` | 🇮🇹 | Italiano | Italy | quarantadue |
22+
| `in`, `en-in` | 🇮🇳 | Indian English | India | forty-two |
2423
| `es`, `es-es` | 🇪🇸 | European Spanish | Spain | cuarenta y dos |
25-
| `dk`, `da-dk` | 🇩🇰 | Danish | Denmark | toogfyrre |
24+
| `dk`, `da-dk` | 🇩🇰 | Danish | Denmark | toogfyrre |
2625
| `se`, `sv-se` | 🇸🇪 | Swedish | Sweden | fyrtio-två |
2726
| `nl`, `nl-nl` | 🇳🇱 | Dutch | Netherlands | tweeenveertig |
2827
| `tr`, `tr-tr` | 🇹🇷 | Turkish | Turkey | kırk iki |
@@ -50,6 +49,9 @@ nonante-deux
5049
$ number-to-words --lang=it 42
5150
quarantadue
5251

52+
$ number-to-words --lang=in 42
53+
forty-two
54+
5355
$ number-to-words --lang=es 42
5456
cuarenta y dos
5557

@@ -149,6 +151,7 @@ MCCCXXXVII
149151
seribu tiga ratus tiga puluh tujuh
150152

151153
$ number-to-words --lang=all 1234567890
154+
one arab twenty-three crore forty-five lakh sixty-seven thousand eight hundred ninety
152155
one billion two hundred thirty-four million five hundred sixty-seven thousand eight hundred ninety
153156
un milliard deux cent trente-quatre millions cinq cent soixante-sept mille huit cent quatre-vingt-dix
154157
un milliard deux cent trente-quatre millions cinq cent soixante-sept mille huit cent nonante
@@ -204,6 +207,7 @@ AVAILABLE LANGUAGES:
204207
Belgian French (fr-be, fr_BE, belgian) 🇧🇪
205208
French (fr, fr-fr, fr_FR, french) 🇫🇷
206209
Italian (it, it-it, it_IT, italian) 🇮🇹
210+
Indian English (in, en-in, indian) 🇮🇳
207211
Roman Numbers (with Unicode) (roman-unicode)
208212
Danish (da-dk, da_DK, danish) 🇩🇰
209213
Swedish (sv-se, sv_SE, swedish) 🇸🇪

en-in.go

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package ntw
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
func init() {
9+
// register the language
10+
Languages["en-in"] = Language{
11+
Name: "Indian English",
12+
Aliases: []string{"en", "en-in", "indian", "english"},
13+
Flag: "🇮🇳",
14+
15+
IntegerToWords: IntegerToEnIn,
16+
}
17+
}
18+
19+
// IntegerToEnIn converts an integer to Indian English words
20+
func IntegerToEnIn(input int) string {
21+
var indianMegas = []string{"", "thousand", "lakh", "crore", "arab", "kharab", "neel", "padma", "shankh", "mahashankh"}
22+
var indianUnits = []string{"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}
23+
var indianTens = []string{"", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}
24+
var indianTeens = []string{"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}
25+
26+
//log.Printf("Input: %d\n", input)
27+
words := []string{}
28+
29+
if input < 0 {
30+
words = append(words, "minus")
31+
input *= -1
32+
}
33+
34+
// split integer in hybrids
35+
var hybrids []int
36+
hybrids = integerToDHybrid(input)
37+
38+
39+
// log.Printf("Hybrids: %v\n", hybrids)
40+
41+
// zero is a special case
42+
if len(hybrids) == 0 {
43+
return "zero"
44+
}
45+
46+
// iterate over hybrids
47+
for idx := len(hybrids) - 1; idx >= 0; idx-- {
48+
hybrid := hybrids[idx]
49+
//log.Printf("hybrid: %d (idx=%d)\n", hybrid, idx)
50+
51+
// nothing todo for empty hybrid
52+
if hybrid == 0 {
53+
continue
54+
}
55+
56+
// three-digits
57+
hundreds := hybrid / 100 % 10
58+
tens := hybrid / 10 % 10
59+
units := hybrid % 10
60+
//log.Printf("Hundreds:%d, Tens:%d, Units:%d\n", hundreds, tens, units)
61+
if hundreds > 0 {
62+
words = append(words, indianUnits[hundreds], "hundred")
63+
}
64+
65+
if tens == 0 && units == 0 {
66+
goto hybridEnd
67+
}
68+
69+
switch tens {
70+
case 0:
71+
words = append(words, indianUnits[units])
72+
case 1:
73+
words = append(words, indianTeens[units])
74+
break
75+
default:
76+
if units > 0 {
77+
word := fmt.Sprintf("%s-%s", indianTens[tens], indianUnits[units])
78+
words = append(words, word)
79+
} else {
80+
words = append(words, indianTens[tens])
81+
}
82+
break
83+
}
84+
85+
hybridEnd:
86+
// mega
87+
if mega := indianMegas[idx]; mega != "" {
88+
words = append(words, mega)
89+
}
90+
}
91+
92+
//log.Printf("Words length: %d\n", len(words))
93+
return strings.Join(words, " ")
94+
}

utils.go

+19
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,22 @@ func integerToTriplets(number int) []int {
1010

1111
return triplets
1212
}
13+
14+
func integerToDHybrid(number int) []int {
15+
hybrid := []int{}
16+
17+
startHybrid := false
18+
for number > 0 {
19+
if !startHybrid{
20+
hybrid = append(hybrid, number%1000)
21+
number = number / 1000
22+
startHybrid = true
23+
} else {
24+
hybrid = append(hybrid, number%100)
25+
number = number / 100
26+
}
27+
28+
}
29+
30+
return hybrid
31+
}

0 commit comments

Comments
 (0)