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
+8.2k Golang : Generate Codabar
+4.2k Unix/Linux : secure copying between servers with SCP command examples
+6.3k Golang : Example of custom handler for Gorilla's Path usage.
+9.1k Golang : Read file and convert content to string
+7.8k Golang : Gonum standard normal random numbers example
+5.2k Javascript : Get operating system and browser information
+6.5k Golang : Example of how to detect which type of script a word belongs to
+5.7k Golang : How to setup a disk space used monitoring service with Telegram bot
+6.3k Golang : Process json data with Jason package
+14.9k Golang : Get sub string example
+7k Golang : How to feed or take banana with Gorilla Web Toolkit Session package
+9.5k Golang : Flip coin example