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