-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathfr-fr.go
142 lines (126 loc) · 3.3 KB
/
fr-fr.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package ntw
import (
"fmt"
"strings"
)
func init() {
// register the language
Languages["fr-fr"] = Language{
Name: "French",
Aliases: []string{"fr", "fr-fr", "fr_FR", "french"},
Flag: "🇫🇷",
IntegerToWords: IntegerToFrFr,
}
}
// IntegerToFrFr converts an integer to French words
func IntegerToFrFr(input int) string {
var frenchMegas = []string{"", "mille", "million", "milliard", "billion", "billiard", "trillion", "trilliard", "quadrillion", "quadrilliard", "quintillion", "quintilliard"}
var frenchUnits = []string{"", "un", "deux", "trois", "quatre", "cinq", "six", "sept", "huit", "neuf"}
var frenchTens = []string{"", "dix", "vingt", "trente", "quarante", "cinquante", "soixante", "soixante", "quatre-vingt", "quatre-vingt"}
var frenchTeens = []string{"dix", "onze", "douze", "treize", "quatorze", "quinze", "seize", "dix-sept", "dix-huit", "dix-neuf"}
//log.Printf("Input: %d\n", input)
words := []string{}
if input < 0 {
words = append(words, "moins")
input *= -1
}
// split integer in triplets
triplets := integerToTriplets(input)
//log.Printf("Triplets: %v\n", triplets)
// zero is a special case
if len(triplets) == 0 {
return "zéro"
}
// iterate over triplets
for idx := len(triplets) - 1; idx >= 0; idx-- {
triplet := triplets[idx]
//log.Printf("Triplet: %d (idx=%d)\n", triplet, idx)
// nothing todo for empty triplet
if triplet == 0 {
continue
}
// special cases
if triplet == 1 && idx == 1 {
words = append(words, "mille")
continue
}
// three-digits
hundreds := triplet / 100 % 10
tens := triplet / 10 % 10
units := triplet % 10
//log.Printf("Hundreds:%d, Tens:%d, Units:%d\n", hundreds, tens, units)
if hundreds > 0 {
if hundreds == 1 {
words = append(words, "cent")
} else {
if tens == 0 && units == 0 {
words = append(words, frenchUnits[hundreds], "cents")
goto tripletEnd
} else {
words = append(words, frenchUnits[hundreds], "cent")
}
}
}
if tens == 0 && units == 0 {
goto tripletEnd
}
switch tens {
case 0:
words = append(words, frenchUnits[units])
case 1:
words = append(words, frenchTeens[units])
break
case 7:
switch units {
case 1:
words = append(words, frenchTens[tens], "et", frenchTeens[units])
break
default:
word := fmt.Sprintf("%s-%s", frenchTens[tens], frenchTeens[units])
words = append(words, word)
break
}
break
case 8:
switch units {
case 0:
words = append(words, frenchTens[tens]+"s")
break
default:
word := fmt.Sprintf("%s-%s", frenchTens[tens], frenchUnits[units])
words = append(words, word)
break
}
break
case 9:
word := fmt.Sprintf("%s-%s", frenchTens[tens], frenchTeens[units])
words = append(words, word)
break
default:
switch units {
case 0:
words = append(words, frenchTens[tens])
break
case 1:
words = append(words, frenchTens[tens], "et", frenchUnits[units])
break
default:
word := fmt.Sprintf("%s-%s", frenchTens[tens], frenchUnits[units])
words = append(words, word)
break
}
break
}
tripletEnd:
// mega
mega := frenchMegas[idx]
if mega != "" {
if mega != "mille" && triplet > 1 {
mega += "s"
}
words = append(words, mega)
}
}
//log.Printf("Words length: %d\n", len(words))
return strings.Join(words, " ")
}