num2words

package module
v0.0.0-...-a438123 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2024 License: MIT Imports: 1 Imported by: 0

README

num2words

num2words - Numbers to words converter in Go (Golang)

Usage

First, go get package num2words go get github.com/celediel/num2words@localized

Then, import package num2words

import "github.com/celediel/num2words"

Convert number

  str := num2words.Convert(17, "en") // outputs "seventeen"
  str := num2words.Convert(17, "es") // outputs "diecisiete"
  ...
  str := num2words.Convert(1024, "en") // outputs "one thousand twenty four"
  str := num2words.Convert(1024, "es") // outputs "mil veinticuatro"
  ...
  str := num2words.Convert(-123, "en") // outputs "minus one hundred twenty three"
  str := num2words.Convert(-123, "es") // outputs "menos ciento veintitrés"

Convert number with " and " between number groups:

  str := num2words.ConvertAnd(514, "en") // outputs "five hundred and fourteen"
  str := num2words.ConvertAnd(514, "es") // outputs "quinientos y quince"
  ...
  str := num2words.ConvertAnd(123, "en") // outputs "one hundred and twenty three"
  str := num2words.ConvertAnd(123, "es") // outputs "ciento y veintitrés"

Documentation

Overview

Package num2words implements numbers to words converter.

Index

Constants

This section is empty.

Variables

View Source
var English = Lang{
	Locale: "en",
	SmallNumbers: []string{
		"zero", "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",
	},
	Hundreds: []string{
		"", "one hundred", "two hundred", "three hundred", "four hundred", "five hundred", "six hundred", "seven hundred", "eight hundred", "nine hundred",
	},
	ScaleNumbers: []string{
		"", "thousand", "million", "billion",
	},
	And:       "and",
	Separator: "-",
	Minus:     "minus",
}
View Source
var Spanish = Lang{
	Locale: "en",
	SmallNumbers: []string{
		"cero", "uno", "dos", "tres", "cuatro",
		"cinco", "seis", "siete", "ocho", "nueve",
		"diez", "once", "doce", "trece", "catorce",
		"quince", "dieciséis", "diecisiete", "dieciocho",
		"diecinueve", "veinte", "veintiuno", "veintidós",
		"veintitrés", "veinticuatro", "veinticinco",
		"veintiséis", "veintisiete", "veintiocho", "veintinueve",
	},
	Tens: []string{
		"", "", "", "treinta", "cuarenta", "cincuenta",
		"sesenta", "setenta", "ochenta", "noventa",
	},
	Hundreds: []string{
		"cien", "ciento", "doscientos", "trescientos", "cuatrocientos", "quinientos", "seiscientos", "setecientos", "ochocientos", "novecientos",
	},
	ScaleNumbers: []string{
		"", "mil", "millón", "billón",
	},
	And:       "y",
	Separator: " y ",
	Minus:     "menos",
}

Functions

func Convert

func Convert(number int, locale Locale) string

Convert converts number into the words representation.

func ConvertAnd

func ConvertAnd(number int, locale Locale) string

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

Types

type Lang

type Lang struct {
	Locale       Locale
	SmallNumbers []string
	Tens         []string
	Hundreds     []string
	ScaleNumbers []string
	And          string
	Separator    string
	Minus        string
}

type Locale

type Locale string
const (
	LocaleEn Locale = "en"
	LocaleEs Locale = "es"
)

Jump to

Keyboard shortcuts

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