Introduction
In this tutorial, we will explore how to convert a number into its word representation using the Go programming language. This is a useful utility for applications that need to display numbers as text, such as for check writing systems, invoice generators, or financial applications.
Objective
The goal is to create a simple Go program that takes an integer input and converts it into its corresponding English words. The program will handle numbers up to a specified range and output the result in a readable format, such as “One Hundred Twenty Three” or “Four Million Two Hundred Fifty Six”.
Go Code: Number to Words Converter
package main
import (
"fmt"
"strings"
)
var belowTwenty = []string{
"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen",
}
var tens = []string{
"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety",
}
var thousands = []string{
"", "Thousand", "Million", "Billion", "Trillion",
}
// Function to convert a number less than 1000 to words
func convertThreeDigits(num int) string {
if num == 0 {
return ""
}
result := ""
if num >= 100 {
result += belowTwenty[num/100] + " Hundred "
num %= 100
}
if num >= 20 {
result += tens[num/10] + " "
num %= 10
}
if num > 0 {
result += belowTwenty[num] + " "
}
return strings.TrimSpace(result)
}
// Function to convert a number to words
func numberToWords(num int) string {
if num == 0 {
return "Zero"
}
res := ""
idx := 0
for num > 0 {
if num%1000 != 0 {
res = convertThreeDigits(num%1000) + " " + thousands[idx] + " " + res
}
num /= 1000
idx++
}
return strings.TrimSpace(res)
}
func main() {
var num int
fmt.Print("Enter a number: ")
fmt.Scan(&num)
result := numberToWords(num)
fmt.Println("Number in words:", result)
}
Program Explanation
This Go program takes an integer as input and converts it into its word representation. Here’s a breakdown of the key components:
- belowTwenty: An array of the first twenty numbers in English.
- tens: An array for multiples of ten (20, 30, …, 90).
- thousands: An array for larger groups of numbers such as thousand, million, billion.
- convertThreeDigits: A helper function that converts numbers less than 1000 into words.
- numberToWords: The main function that loops through the number, breaking it down into groups of three digits (thousands, millions, etc.), and converts each group into words.
The program reads an integer input from the user and passes it to the numberToWords
function. The function then returns the corresponding word representation, which is printed to the console.
How to Run the Program
- Install Go from here.
- Save the provided code in a file named
main.go
. - Open a terminal or command prompt.
- Navigate to the directory where the
main.go
file is located. - Run the following command to execute the program:
go run main.go
- Enter a number when prompted, and the program will output the number in words.