Golang
Golang

 

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

  1. Install Go from here.
  2. Save the provided code in a file named main.go.
  3. Open a terminal or command prompt.
  4. Navigate to the directory where the main.go file is located.
  5. Run the following command to execute the program:
    go run main.go
  6. Enter a number when prompted, and the program will output the number in words.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)