-
Notifications
You must be signed in to change notification settings - Fork 2
/
PersianNumberToWord.kt
137 lines (133 loc) · 3.96 KB
/
PersianNumberToWord.kt
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
import java.math.BigDecimal
class PersianNumberToWord (private val num: BigDecimal?,private val unit: String){
fun create(): String {
return onDo(num, 0) + " " + unit
}
private val yekan = arrayOf(
" یک ",
" دو ",
" سه ",
" چهار ",
" پنج ",
" شش ",
" هفت ",
" هشت ",
" نه "
)
private val dahgan = arrayOf(
" بیست ",
" سی ",
" چهل ",
" پنجاه ",
" شصت ",
" هفتاد ",
" هشتاد ",
" نود "
)
private val sadgan = arrayOf(
" یکصد ",
" دویست ",
" سیصد ",
" چهارصد ",
" پانصد ",
" ششصد ",
" هفتصد ",
" هشتصد ",
" نهصد "
)
private val dah = arrayOf(
" ده ",
" یازده ",
" دوازده ",
" سیزده ",
" چهارده ",
" پانزده ",
" شانزده ",
" هفده ",
" هیجده ",
" نوزده "
)
private fun onDo(number: BigDecimal?, levels: Int): String {
var num = number
var level = levels
if (num == null) {
return ""
}
// convert negative number to positive and get wordify value
if (num < BigDecimal(0)) {
num = num.negate()
return "منفی " + onDo(num, level)
}
if (num.compareTo(BigDecimal(0)) == 0) {
return if (level == 0) {
"صفر"
} else {
""
}
}
var result = ""
if (level > 0) {
result += " و "
level -= 1
}
when {
num < BigDecimal(10) -> {
result += yekan[num.add(BigDecimal(1).negate()).toInt()]
}
num < BigDecimal(20) -> {
result += dah[num.add(BigDecimal(10).negate()).toInt()]
}
num < BigDecimal(100) -> {
result += dahgan[num.divide(BigDecimal(10)).add(BigDecimal(2).negate())
.toInt()] + onDo(
num.remainder(BigDecimal(10)),
level + 1
)
}
num < BigDecimal(1000) -> {
result += sadgan[num.divide(BigDecimal(100)).add(BigDecimal(1).negate())
.toInt()] + onDo(
num.remainder(BigDecimal(100)),
level + 1
)
}
num < BigDecimal(1000000) -> {
result += onDo(
num.divide(BigDecimal(1000)),
level
) + " هزار " + onDo(
num.remainder(BigDecimal(1000)),
level + 1
)
}
num < BigDecimal(1000000000) -> {
result += onDo(
num.divide(BigDecimal(1000000)),
level
) + " میلیون " + onDo(
num.remainder(BigDecimal(1000000)),
level + 1
)
}
num < BigDecimal(java.lang.Long.valueOf("1000000000000")) -> {
result += onDo(
num.divide(BigDecimal("1000000000".toLong())),
level
) + " میلیارد " + onDo(
num.remainder(BigDecimal("1000000000".toLong())),
level + 1
)
}
num < BigDecimal(java.lang.Long.valueOf("1000000000000000")) -> {
result += onDo(
num.divide(BigDecimal("1000000000000".toLong())),
level
) + " تریلیارد " + onDo(
num.remainder(BigDecimal("1000000000000".toLong())),
level + 1
)
}
}
return result
}
}