num2words

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2024 License: MIT Imports: 3 Imported by: 26

README

num2words

Build Status GoDoc

num2words - Numbers to words converter in Go (Golang)

Usage

First, import package num2words

import github.com/divan/num2words

Convert number

  str := num2words.Convert(17) // outputs "seventeen"
  ...
  str := num2words.Convert(1024) // outputs "one thousand twenty four"
  ...
  str := num2words.Convert(-123) // outputs "minus one hundred twenty three"

Convert number with " and " between number groups:

  str := num2words.ConvertAnd(514) // outputs "five hundred and fourteen"
  ...
  str := num2words.ConvertAnd(123) // outputs "one hundred and twenty three"

Documentation

Overview

Package num2words implements numbers to words converter.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Convert

func Convert(number int) string

Convert converts number into the words representation.

Example
fmt.Println(num2words.Convert(11))
fmt.Println(num2words.Convert(123))
fmt.Println(num2words.Convert(-99))
Output:

eleven
one hundred twenty-three
minus ninety-nine

func ConvertAnd

func ConvertAnd(number int) string

ConvertAnd converts number into the words representation with " and " added between number groups.

Example
fmt.Println(num2words.ConvertAnd(123))
fmt.Println(num2words.ConvertAnd(514))
Output:

one hundred and twenty-three
five hundred and fourteen

func ConvertFloat added in v1.0.1

func ConvertFloat(number float64, precision Precision) string

ConvertFloat converts float number into words representation. precision controls decimal places (-1 for automatic precision)

Example
fmt.Println(num2words.ConvertFloat(123.45, 2))
fmt.Println(num2words.ConvertFloat(-0.42, 2))
fmt.Println(num2words.ConvertFloat(1.50, num2words.AutoPrecision))
Output:

one hundred twenty-three point forty-five
minus zero point forty-two
one point five

func ConvertFloatAnd added in v1.0.1

func ConvertFloatAnd(number float64, precision Precision) string

ConvertFloatAnd converts float number into words representation with " and " added between number groups. precision controls decimal places (-1 for automatic precision)

Example
fmt.Println(num2words.ConvertFloatAnd(123.45, 2))
fmt.Println(num2words.ConvertFloatAnd(1234.5678, 4))
Output:

one hundred and twenty-three point forty-five
one thousand two hundred and thirty-four point five thousand six hundred and seventy-eight

Types

type Precision added in v1.0.1

type Precision int

Precision represents the number of decimal places to convert -1 means automatic precision (removes trailing zeros)

const (
	AutoPrecision Precision = -1
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL