Documentation ¶
Overview ¶
Package numwords is a utility that converts textual numbers to their actual numeric values. The converted numbers can be parsed out as strings, integers, or floats as desired.
Source: https://github.com/rodaine/numwords
Example ¶
s := "I've got three apples and two and a half bananas" fmt.Println(ParseString(s)) s = "My chili won second place at the county fair" fmt.Println(ParseString(s)) i, _ := ParseInt("fourteen ninety two") fmt.Println(i) f, _ := ParseFloat("eight and three quarters") fmt.Println(f)
Output: I've got 3 apples and 2.5 bananas My chili won 2nd place at the county fair 1492 8.75
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoNumbers is returned if the a value for numbers is requested but there are // no number values inside of it ErrNoNumbers = errors.New("the input contains no number values") // ErrManyNumbers is returned if the value for numbers is requested but there are // more than one number values inside of it ErrManyNumbers = errors.New("the input contains more than one number") // ErrNonNumber is returned if ParseInt or ParseFloat encounters a non-number in // the input string. ErrNonNumber = errors.New("the string contains a non-number") )
Functions ¶
func IncludeSecond ¶
func IncludeSecond(include bool)
IncludeSecond toggles whether or not "second" should be included in the interpreted words. If true "second" will be read as "2nd", otherwise the word will be ignored. The default is set to true.
Example ¶
s := "My chili won second place at the county fair" fmt.Println(ParseString(s)) s = "One second ago" IncludeSecond(false) fmt.Println(ParseString(s))
Output: My chili won 2nd place at the county fair 1 second ago
func ParseFloat ¶
ParseFloat reads a text string and converts it to its float value. An error is returned if the if the string cannot be resolved to a single float value.
Example ¶
f, _ := ParseFloat("eight and three quarters") fmt.Println(f)
Output: 8.75
func ParseInt ¶
ParseInt reads a text string and converts it to its integer value. An error is returned if the if the string cannot be resolved to a single integer value. Fractional portions of the number will be truncated.
Example ¶
i, _ := ParseInt("fourteen ninety two") fmt.Println(i)
Output: 1492
func ParseString ¶
ParseString reads a text string and converts all numbers contained within to their appropriate values. Integers are preserved exactly while floating point numbers are limited to six decimal places. The rest of the string is preserved.
Example ¶
s := "I've got three apples and two and a half bananas" fmt.Println(ParseString(s))
Output: I've got 3 apples and 2.5 bananas
func ParseStrings ¶
ParseStrings performs the same actions as ParseString but operates on a pre- sanitized and split string. This method is exposed for convenience if further processing of the string is required.
Types ¶
This section is empty.