Crate num2words

Source
Expand description

Convert numbers like 42 to forty-two

§Usage

This crate can be either used as a library or a binary.

§Library

Example usage:

use num2words::Num2Words;
assert_eq!(Num2Words::new(42).to_words(), Ok(String::from("forty-two")));

The builder Num2Words can take any of these methods: lang, cardinal, ordinal, ordinal_num, year, and currency.

use num2words::*;
assert_eq!(
    Num2Words::new(42).lang(Lang::French).to_words(),
    Ok(String::from("quarante-deux"))
);
assert_eq!(
    Num2Words::new(42).ordinal().to_words(),
    Ok(String::from("forty-second"))
);
assert_eq!(
    Num2Words::new(42.01).currency(Currency::DOLLAR).to_words(),
    Ok(String::from("forty-two dollars and one cent"))
);

These arguments can be chained.

For more information about the available languages, outputs types and currencies, see Information.

§Binary

This crate provides a command-line interface to run requests on num2words.

Example:

$ num2words 42
forty-two
$ num2words 10 --to UAH --lang uk
десять гривень

You can download the app via the following command:

$ cargo install num2words

You can also change the language via the CLI argument --lang [locale] and provide a specific output type or a currency with the argument --to [cardinal|ordinal|ordinal_num|year|ISO 4217].

For more information about the usage of num2words please refer to the docs or via the following command:

$ num2words --help

§Information

§Supported languages

Here is a list of all of the supported languages:

FlagCodeLocaleLanguage42
🇺🇸🇬🇧Lang::EnglishenEnglishforty-two
🇫🇷🇨🇦Lang::FrenchfrFrenchquarante-deux
🇧🇪🇨🇩Lang::French_BEfr_BEFrench (BE)quarante-deux
🇨🇭Lang::French_CHfr_CHFrench (CH)quarante-deux
🇺🇦Lang::UkrainianukUkrainianсорок два

This list can be expanded! Contributions are welcomed.

§Supported output types

Here is a list of all of the supported outputs types (with the associated command-line interface code):

Library methodCLI argumentExample output
.cardinal()cardinalforty-two (42)
.ordinal()ordinalforty-second (42)
.ordinal_num()ordinal_num42nd (42)
.year()yearnineteen oh-one (1901)
.currency(cur)ISO 4217 codeforty-two dollars and one cent (42.01)

§Supported currencies

Three-letter enum variants corresponds to the currency’s ISO 4217 code, but there are exceptions to accomodate generic terminologies: DINAR, DOLLAR, PESO and RIYAL.

A summary of all of the supported currencies are available in the documentation of Currency.

§About

This library is widely inspired by Savoir-faire Linux’s Python lib.

Structs§

Num2Words
Builder for num2words

Enums§

Currency
Defines currencies
Lang
Languages available in num2words
Num2Err
Error type returned by the builder