-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathpt-pt.go
129 lines (112 loc) · 3.21 KB
/
pt-pt.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
package ntw
import (
"fmt"
"strings"
)
func init() {
// register the language
Languages["pt-pt"] = Language{
Name: "Portuguese (Portugal)",
Aliases: []string{"pt", "pt-pt", "pt_PT", "portuguese"},
Flag: "🇵🇹",
IntegerToWords: IntegerToPtPt,
}
}
func IntegerToPtPt(input int) string {
var portugueseMegasSingular = []string{"", "mil", "milhão", "mil milhões", "bilião"}
var portugueseMegasPlural = []string{"", "mil", "milhões", "mil milhões", "bilhões"}
var portugueseUnits = []string{"", "um", "dois", "três", "quatro", "cinco", "seis", "sete", "oito", "nove"}
var portugueseHundreds = []string{"", "cem", "duzentos", "trezentos", "quatrocentos", "quinhentos", "seiscentos", "setecentos", "oitocentos", "novecentos", "cento"}
var portugueseTens = []string{"", "dez", "vinte", "trinta", "quarenta", "cinquenta", "sessenta", "setenta", "oitenta", "noventa"}
var portugueseTeens = []string{"dez", "onze", "doze", "treze", "catorze", "quinze", "dezasseis", "dezasete", "dezoito", "dezanove"}
//log.Printf("Input: %d\n", input)
words := []string{}
if input < 0 {
words = append(words, "menos")
input *= -1
}
triplets := integerToTriplets(input)
switch {
case len(triplets) == 0:
return "zero"
}
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
}
// 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 && units == 0 && tens == 0 {
var word string
if idx == 0 && len(words) != 0 {
word = fmt.Sprintf("e %s", portugueseHundreds[hundreds])
} else {
word = fmt.Sprintf("%s", portugueseHundreds[hundreds])
}
words = append(words, word)
} else if hundreds > 0 {
if hundreds == 1 {
hundreds = 10
}
word := fmt.Sprintf("%s e", portugueseHundreds[hundreds])
words = append(words, word)
}
if tens == 0 && units == 0 {
goto tripletEnd
}
switch tens {
case 0:
words = append(words, portugueseUnits[units])
case 1:
word := fmt.Sprintf("%s", portugueseTeens[units])
words = append(words, word)
break
default:
if units > 0 {
word := fmt.Sprintf("%s e %s", portugueseTens[tens], portugueseUnits[units])
words = append(words, word)
} else {
word := fmt.Sprintf("%s", portugueseTens[tens])
words = append(words, word)
}
break
}
tripletEnd:
switch triplet {
case 1:
if mega := portugueseMegasSingular[idx]; mega != "" {
if idx == 4 {
sum := 0
for i := 0; i < len(triplets)-1; i++ {
sum += triplets[i]
}
if sum == 0 {
} else {
words = append(words, "um")
}
} else if idx == 1 && portugueseUnits[idx] == words[0] {
words = words[1:]
}
word := fmt.Sprintf("%s", mega)
words = append(words, word)
}
break
default:
if mega := portugueseMegasPlural[idx]; mega != "" {
if idx == 1 && portugueseUnits[idx] == words[0] {
words = words[1:]
}
word := fmt.Sprintf("%s", mega)
words = append(words, word)
}
break
}
}
return strings.Join(words, " ")
}