Golang. Number into words


Let’s write a program that prints numbers as text. Let’s limit it to a range of 1 to 1000.

Here is the first example of code, a fuzzy one:

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func n10_19(n int) {
	switch n {
	case 1:
		fmt.Print("elven ")
	case 2:
		fmt.Print("twelve ")
	case 3:
		fmt.Print("thirteen ")
	case 4:
		fmt.Print("fourteen ")
	case 5:
		fmt.Print("fifteen ")
	case 6:
		fmt.Print("sixsteen ")
	case 7:
		fmt.Print("seventeen ")
	case 8:
		fmt.Print("eighteen ")
	case 9:
		fmt.Print("nineteen ")
	}
}

func n20_90(n int) {
	switch n {
	case 2:
		fmt.Print("twenty ")
	case 3:
		fmt.Print("thirty ")
	case 4:
		fmt.Print("fourty ")
	case 5:
		fmt.Print("fifty ")
	case 6:
		fmt.Print("sixsty ")
	case 7:
		fmt.Print("seventy ")
	case 8:
		fmt.Print("eighty ")
	case 9:
		fmt.Print("ninety ")
	}
}

func n1_9(n int) {
	switch n {
	case 1:
		fmt.Print("one ")
	case 2:
		fmt.Print("two ")
	case 3:
		fmt.Print("three ")
	case 4:
		fmt.Print("four ")
	case 5:
		fmt.Print("five ")
	case 6:
		fmt.Print("six ")
	case 7:
		fmt.Print("seven ")
	case 8:
		fmt.Print("eight ")
	case 9:
		fmt.Print("nine ")
	case 10:
		fmt.Print("thousand ")
	}
}

func input(r *bufio.Reader) {
	input, err := r.ReadString('\n')
	if err != nil {
		panic(err)
	}

	s := strings.TrimSpace(input)
	n, _ := strconv.Atoi(s)

	if n/100 > 0 {
		n1_9(n / 100)
		print("hundred ")
		n %= 100
	}

	if n/10 > 0 {
		if n/10 == 1 { // 10 - 19
			n10_19(n % 10)
		} else {
			n20_90(n / 10)
			n %= 10
			if n == 0 {
				return
			} else {
				n1_9(n)
			}
		}
		return
	}

	if n%10 > 0 {
		n1_9(n % 10)
	}
}

func main() {

	r := bufio.NewReader(os.Stdin)

	fmt.Println("Enter number: ")
	input(r)

}

Second, polished version:

package main

import (
	"fmt"
)

func main() {
	var num int
	fmt.Print("Enter a number between 0 and 1000: ")
	fmt.Scan(&num)

	if num < 0 || num >= 1000 {
		fmt.Println("Invalid input. Please enter a number between 0 and 1000.")
	} else {
		fmt.Println(numberToWords(num))
	}
}

func numberToWords(num int) string {
	ones := []string{"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}
	tens := []string{"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}
	if num < 20 {
		return ones[num]
	} else if num < 100 {
		return tens[num/10] + " " + ones[num%10]
	} else {
		return ones[num/100] + " hundred " + numberToWords(num%100)
	}
}

This program uses a recursive function numberToWords() to convert the number to words. The function takes an integer as input, and returns a string containing the number as words. The function first checks if the number is less than 20, in which case it returns the corresponding word from the ones array. If the number is greater than or equal to 20 and less than 100, it returns the corresponding word from the tens array concatenated with the corresponding word from the ones array. If the number is greater than or equal to 100, it returns the corresponding word from the ones array for the hundreds digit concatenated with “hundred” and the result of calling numberToWords() on the remaining two digits.


This entry was posted in Go (en). Bookmark the permalink.

Leave a Reply

🇬🇧 Attention! Comments with URLs/email are not allowed.
🇷🇺 Комментарии со ссылками/email удаляются автоматически.