numberconverter

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2024 License: MIT Imports: 7 Imported by: 0

README

numberconverter

⚠️ numberconverter is currently pre-release. The API is unstable and some methods may not be correct.

Go Reference


numberconverter provides two simple methods for converting between English and Integers in Go.

numberconverter.Etoi("One hundred thousand, three hundred and fifty two") // 100_352
numberconverter.Itoe(100352) // one hundred thousand three hundred fifty-two

Style agnostic

It doesn't matter how you write your English numbers, they should parse in most cases. There is no prescribed style.

numberconverter.Etoi("Three hundred and forty two million") // 342_000_000
numberconverter.Etoi("nineteen thirty six") // 1936
numberconverter.Etoi("one hundred two hundred") // 100200
numberconverter.Etoi("three hundred, fourty two million") // 342_000_000

Batteries included

Methods for finding and replacing English numbers in your strings are provided.

numberconverter.FindAllEnglishNumber("Fifty five dogs. Three hundred and twenty three geese.") // {"Fifty five", "Three hundred and twenty three"}
numberconverter.EtoiReplaceAll("If we talk about dogs, I have three. Two of them live in a kennel") // "If we talk about dogs, I have 3. 2 of them live in a kennel"

Generic

Want to parse int8? No problem!

numberconverter.EtoiGeneric[int8]("Fifty five") // 55

Numbers above the given integer's maximum will produce unexpected results—be careful!

numberconverter.EtoiGeneric[int8]("Fifty hundred and fifty two million") // 0

Zero configuration

No need to pass around an instance of a struct to use the converter. The package provides two simple methods that may be used globally.

Zero dependency

Nourish your codebase with pure Go goodness.

Documentation

Overview

Package numberconverter converts contemporary English language numbers ("negative one million three hundred thousand") into signed integers (-1_300_000), and back. numtowords and wordstonum.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Etoi

func Etoi(buf []rune) (int64, error)

Etoi (English to Integer) will convert the first instance of an english language number string into an int64. An input of "five" would return 5. This function may not error on some English syntax errors. It assumes correct English.

func EtoiGeneric

func EtoiGeneric[T util.Integer](buf []rune) (T, error)

EtoiGeneric also converts an english language string into a signed integer, but is generic. This function uses bare type coercion and may result in funky numbers being returned! If you want to guarantee a conversion, consider the non generic version.

func EtoiReplaceAll added in v0.3.0

func EtoiReplaceAll(str string) string

EtoiReplaceAll wraps EtoiReplaceAllFunc by always replacing matches.

func EtoiReplaceAllFunc added in v0.3.0

func EtoiReplaceAllFunc(str string, f func(i int64) bool) string

EtoiReplaceAllFunc allows a user replace all the instances of an English number in a given string with its numerical representation if the given function returns true. The given function is passed the integer representation of the matched English number.

func EtoiString added in v0.3.0

func EtoiString(str string) (int64, error)

EtoiString wraps Etoi, converting the given string to a rune array

func EtoiStringGeneric added in v0.3.0

func EtoiStringGeneric[T util.Integer](str string) (T, error)

EtoiStringGeneric allows generic usage of EtoiString. Like EtoiGeneric, it also uses bare type coercion: use types with a lower maximum than int64 at your own risk!

func FindAllEnglishNumber added in v0.3.0

func FindAllEnglishNumber(str string, n int) []string

FindAllEnglishNumber wraps FindAllEnglishNumberIndex but returns a list of strings rather than indexes.

func FindAllEnglishNumberIndex added in v0.3.0

func FindAllEnglishNumberIndex(str string, n int) [][]int

FindAllEnglishNumberIndex searches a given string for English numbers, returning their indexes in an array. The parameter 'n' specifies the maximum number of numbers returned. If n=-1, all numbers are returned.

func FindEnglishNumber added in v0.3.0

func FindEnglishNumber(str string) string

FindEnglishNumber wraps FindAllEnglishNumber with n=1

func FindEnglishNumberIndex added in v0.3.0

func FindEnglishNumberIndex(str string) []int

FindEnglishNumberIndex wraps FindAllEnglishNumberIndex with n=1

func Itoe

func Itoe(num int64) string

Itoe (Integer to English) will convert an int64 into an English language string. For example, an input of 5 would produce "five". The style of the output is always consistent—lower case, no 'and', and hyphenation of numbers 21 to 99.

func ItoeGeneric

func ItoeGeneric[T util.Integer](num T) string

Function ItoeGeneric performs the same function as Itoe but is generic.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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