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
+9.2k Golang : Quadratic example
+8.7k Golang : Serving HTTP and Websocket from different ports in a program example
+18.5k Golang : Check whether a network interface is up on your machine
+4.4k Python : Find out the variable type and determine the type with simple test
+5.4k Golang : Find change in a combination of coins example
+4.5k Fix Google Analytics Redundant Hostnames problem
+3.8k Javascript : Empty an array example
+7.3k Golang : Getting Echo framework StartAutoTLS to work
+35.9k Golang : Convert(cast) int64 to string
+11.1k Golang : Concurrency and goroutine example
+9.7k Golang : Edge detection with Sobel method
+18.8k Golang : Fix cannot download, $GOPATH not set error