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
| func numberToWords(num int) string { res := convertHundred(num % 1000) v := []string{"Thousand", "Million", "Billion"} for i := 0; i < 3; i++ { num /= 1000 if num%1000 > 0 { res = convertHundred(num%1000) + " " + v[i] + " " + res } } if res == "" { return "Zero" } return strings.TrimSpace(res) }
func convertHundred(num int) string { under20 := []string{ "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", } decimal := []string{ "", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety", } res := "" a, b, c := num/100, num%100, num%10 if b < 20 { res = under20[b] } else { res = decimal[b/10] if c > 0 { res += " " + under20[c] } } if a > 0 { res = under20[a] + " Hundred " + res } return strings.TrimSpace(res) }
|