-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenglish.go
106 lines (85 loc) · 2.24 KB
/
english.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
95
96
97
98
99
100
101
102
103
104
105
106
package n2w
import (
"fmt"
"math"
"strconv"
"strings"
)
func ToEnglishWords(number float64) (string, error) {
result := ""
strNumber := fmt.Sprintf("%.2f", number)
sections := strings.Split(strNumber, ".")
if integral, err := strconv.Atoi(sections[0]); err != nil {
return "", err
} else {
result += withEnglishStrategy(integral)
}
if fractional, err := strconv.Atoi(sections[1]); err != nil {
return "", err
} else if fractional > 0 {
result += " POINT "
result += withEnglishStrategy(fractional)
}
return result, nil
}
func withEnglishStrategy(number int) string {
result := ""
units := []string{"", "ONE ", "TWO ", "THREE ", "FOUR ", "FIVE ", "SIX ", "SEVEN ", "EIGHT ", "NINE "}
tens := []string{"", "TEN ", "TWENTY ", "THIRTY ", "FORTY ", "FIFTY ", "SIXTY ", "SEVENTY ", "EIGHTY ", "NINETY "}
teens := []string{"TEN ", "ELEVEN ", "TWELVE ", "THIRTEEN ", "FOURTEEN ", "FIFTEEN ", "SIXTEEN ", "SEVENTEEN ", "EIGHTEEN ", "NINETEEN "}
others := []string{"", "HUNDERED ", "THOUSAND ", "MILLION ", "BILLION ", "TRILLION ", "QUADRILION "}
hasParentDigit := false
hasTeens := false
if number == 0 {
return "ZERO"
}
if number < 0 {
result += "NEGATIVE "
number *= -1
}
str_number := strconv.Itoa(number)
power := 1
for i := 0; i < len(str_number); i++ {
order := len(str_number[i:]) % 3
number := str_number[i] - '0'
switch order {
// units
case 1:
power = int(math.Ceil(float64(len(str_number[i:])) / 3))
if hasTeens {
result += teens[number]
hasTeens = false
hasParentDigit = true
continue
}
result += units[number]
hasParentDigit = !(number == 0 && !hasParentDigit)
// tens
case 2:
power = int(math.Ceil(float64(len(str_number[i:])) / 3))
if number == 1 {
hasTeens = true
continue
}
result += tens[number]
hasParentDigit = !(number == 0 && !hasParentDigit)
// other
case 0:
if power > 1 && hasParentDigit {
result += others[power]
}
if number > 0 {
result += units[number]
newPower := int(math.Ceil(float64(len(str_number[i:])) / 3))
if newPower > power {
result += others[power]
} else {
result += others[1]
}
power = newPower
}
hasParentDigit = number > 0
}
}
return strings.TrimSpace(result)
}