Golang : How to convert a number to words
I need to translate a given number into words such as 100
to one hundred
. The code below is a translation of Christian d'Heureuse's work at http://www.source-code.biz/snippets/java/13.htm. It shows how to convert a number up to billion into words.
Here you go!
package main
import (
"fmt"
)
var lowNames = []string{"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}
var tensNames = []string{"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}
var bigNames = []string{"thousand", "million", "billion"}
func convert999(num int) string {
s1 := lowNames[num/100] + " hundred"
s2 := convert99(num % 100)
if num <= 99 {
return s2
}
if num0 == 0 {
return s1
} else {
return s1 + " " + s2
}
}
func convert99(num int) string {
if num < 20 {
return lowNames[num]
}
s := tensNames[num/10-2]
if num == 0 {
return s
}
return s + "-" + lowNames[num]
}
func convertNum2Words(num int) string {
if num < 0 {
return "negative " + convertNum2Words(-num)
}
if num <= 999 {
return convert999(num)
}
s := ""
t := 0
for num > 0 {
if num00 != 0 {
s2 := convert999(num % 1000)
if t > 0 {
s2 = s2 + " " + bigNames[t-1]
}
if s == "" {
s = s2
} else {
s = s2 + ", " + s
}
}
num /= 1000
t++
}
return s
}
func main() {
fmt.Println("100 in words : ", convertNum2Words(100))
fmt.Println("0123 in words : ", convertNum2Words(0123))
fmt.Println("-99 in words : ", convertNum2Words(-99))
fmt.Println("54312337 in words : ", convertNum2Words(54312337))
fmt.Println("932476182910 in words : ", convertNum2Words(932476182910))
fmt.Println("-932476182910 in words : ", convertNum2Words(-932476182910))
// because we only convert up to billion, any number exceeding 9 zeros will cause
// runtime error: index out of range
//fmt.Println("9324761829101 in words : " , convertNum2Words(9324761829101))
}
Output:
100 in words : one hundred
0123 in words : eighty-three <------ wrong!
-99 in words : negative ninety-nine
54312337 in words : fifty-four million, three hundred twelve thousand, three hundred thirty-seven
932476182910 in words : nine hundred thirty-two billion, four hundred seventy-six million, one hundred eighty-two thousand, nine hundred ten
-932476182910 in words : negative nine hundred thirty-two billion, four hundred seventy-six million, one hundred eighty-two thousand, nine hundred ten
NOTES:
You need to put in a sanity checker to test if the input number is a sane number. As demonstrated, having a given number that starts with zero will cause the translation to go crazy. For example, the translation of 0123
to eighty-three
just doesn't make sense.
How to write the sanity checker? That's the exercise for this tutorial. ;-)
Reference:
http://www.source-code.biz/snippets/java/13.htm
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+14.3k Golang : On enumeration
+8.1k Golang : Tell color name with OpenCV example
+10k Golang : How to tokenize source code with text/scanner package?
+11.4k Golang : Format numbers to nearest thousands such as kilos millions billions and trillions
+21.5k Golang : GORM create record or insert new record into database example
+6k Golang : Convert Chinese UTF8 characters to Pin Yin
+5k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version
+5.3k Unix/Linux : How to archive and compress entire directory ?
+3.6k Golang : Switch Redis database redis.NewClient
+4.7k Facebook : How to place save to Facebook button on your website