-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathturkish.go
88 lines (76 loc) · 1.87 KB
/
turkish.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
package n2w
import (
"fmt"
"strconv"
"strings"
)
func withTurkishStrategy(number int) string {
result := ""
units := []string{"", "BİR ", "İKİ ", "ÜÇ ", "DÖRT ", "BEŞ ", "ALTI ", "YEDİ ", "SEKİZ ", "DOKUZ "}
tens := []string{"", "ON ", "YİRMİ ", "OTUZ ", "KIRK ", "ELLİ ", "ALTMIŞ ", "YETMİŞ ", "SEKSEN ", "DOKSAN "}
others := []string{"", "BİN ", "MİLYON ", "MİLYAR ", "TRİLYON ", "KATTRİLYON ", "YÜZ "}
hasParentDigit := false
if number == 0 {
return "SIFIR"
}
if number < 0 {
result += "EKSİ "
number *= -1
}
str_number := strconv.Itoa(number)
for i := 0; i < len(str_number); i++ {
order := len(str_number[i:]) % 3
power := len(str_number[i:]) / 3
number := str_number[i] - '0'
switch order {
// units
case 1:
if power == 1 && !hasParentDigit && number == 1 {
result += units[0]
} else {
result += units[number]
}
if number == 0 && !hasParentDigit {
result += others[0]
} else {
result += others[power]
}
hasParentDigit = false
// tens
case 2:
result += tens[number]
hasParentDigit = !(number == 0 && !hasParentDigit)
// others
case 0:
if number == 1 {
result += units[0]
} else {
result += units[number]
}
if number == 0 {
result += others[0]
} else {
result += others[6]
}
hasParentDigit = !(number == 0 && !hasParentDigit)
}
}
return strings.TrimSpace(result)
}
func ToTurkishWords(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 += withTurkishStrategy(integral)
}
if fractional, err := strconv.Atoi(sections[1]); err != nil {
return "", err
} else if fractional > 0 {
result += " NOKTA "
result += withTurkishStrategy(fractional)
}
return result, nil
}