Crate num2en

Source
Expand description

§num2en

This is a crate with functions for converting any integer or decimal number below 2128 (about 340 undecillion) to words.
It supports converting to cardinal and ordinal numbers.

§Functions

To convert any integer n of type X to cardinal number words, call X_to_words(n).

use num2en::*;
assert_eq!(    u8_to_words(1),    "one");
assert_eq!(    i8_to_words(2),    "two");
assert_eq!(   u16_to_words(3),    "three");
assert_eq!(   i16_to_words(4),    "four");
assert_eq!(   u32_to_words(5),    "five");
assert_eq!(   i32_to_words(6),    "six");
assert_eq!(   u64_to_words(70),   "seventy");
assert_eq!(   i64_to_words(71),   "seventy-one");
assert_eq!(  u128_to_words(180),  "one hundred eighty");
assert_eq!(  i128_to_words(211),  "two hundred eleven");
assert_eq!( usize_to_words(1050), "one thousand fifty");
assert_eq!( isize_to_words(2012), "two thousand twelve");

To convert any unsigned integer n of type X to ordinal number words, call X_to_ord_words(n).

assert_eq!(    u8_to_ord_words(1),    "first");
assert_eq!(   u16_to_ord_words(3),    "third");
assert_eq!(   u32_to_ord_words(5),    "fifth");
assert_eq!(   u64_to_ord_words(70),   "seventieth");
assert_eq!(  u128_to_ord_words(180),  "one hundred eightieth");
assert_eq!( usize_to_ord_words(2012), "two thousand twelfth");

To convert any float f of type Y to number words, call Y_to_words(f).

assert_eq!(  f32_to_words(15.2),  Ok("fifteen point two".to_string()));
assert_eq!(  f64_to_words(42.42), Ok("forty-two point four two".to_string()));

To convert a string representation of a number to number words, call str_to_words(&string).

assert_eq!( str_to_words("123.456"),
             Ok("one hundred twenty-three point four five six".to_string()) );

To spell all digits in a string of digits individually, call str_digits_to_words(&digits).

assert_eq!( str_digits_to_words("001247"), Ok("zero zero one two four seven".to_string()) );

This crate has been thoroughly tested, but if you find any function working incorrectly for some input, please open an issue on Github.

Enums§

DigitConversionError
Represents the possible error that can occur when calling str_digits_to_words.
FloatConversionError
Represents the possible errors that can occur when calling f32_to_words or f64_to_words.
StrConversionError
Represents the possible errors that can occur when calling str_to_words.

Functions§

f32_to_words
Converts any* f32 value of a number to a number representation in words.
f64_to_words
Converts any* f64 value of a number to a number representation in words.
i8_to_words
Converts any u8 value to its cardinal number representation in words (one, two, three etc.).
i16_to_words
Converts any i16 value to its cardinal number representation in words (one, two, three etc.).
i32_to_words
Converts any i32 value to its cardinal number representation in words (one, two, three etc.).
i64_to_words
Converts any i64 value to its cardinal number representation in words (one, two, three etc.).
i128_to_words
Converts any i128 value to its cardinal number representation in words (one, two, three etc.).
isize_to_words
Converts any isize value to its cardinal number representation in words (one, two, three etc.).
str_digits_to_words
Converts any string of digits (0-9) to a string of all the digits spelled out individually.
str_to_words
Converts any* string of a (decimal) number to a number representation in words.
u8_to_ord_words
Converts any u8 value to its ordinal number representation in words (first, second, third etc.).
u8_to_words
Converts any u8 value to its cardinal number representation in words (one, two, three etc.).
u16_to_ord_words
Converts any u16 value to its ordinal number representation in words (first, second, third etc.).
u16_to_words
Converts any u16 value to its cardinal number representation in words (one, two, three etc.).
u32_to_ord_words
Converts any u32 value to its ordinal number representation in words (first, second, third etc.).
u32_to_words
Converts any u32 value to its cardinal number representation in words (one, two, three etc.).
u64_to_ord_words
Converts any u64 value to its ordinal number representation in words (first, second, third etc.).
u64_to_words
Converts any u64 value to its cardinal number representation in words (one, two, three etc.).
u128_to_ord_words
Converts any u128 value to its ordinal number representation in words (first, second, third etc.).
u128_to_words
Converts any u128 value to its cardinal number representation in words (one, two, three etc.).
usize_to_ord_words
Converts any usize value to its ordinal number representation in words (first, second, third etc.).
usize_to_words
Converts any usize value to its cardinal number representation in words (one, two, three etc.).