Convert number like 42 to forty-two

Overview

num2words

Convert number 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 arguments: lang, cardinal, ordinal, ordinal_num, year, and currency.

use num2words::*;
assert_eq!(
    Num2Words::new(42).lang(Lang::English).to_words(),
    Ok(String::from("forty-two"))
);
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 EUR
ten euros

You can download the app via the following command:

$ cargo install num2words

You can also change the language via the CLI argument --lang [ISO 639-1 code] 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:

Flag Code ISO 639-1 Language 42
πŸ‡ΊπŸ‡Έ πŸ‡¬πŸ‡§ Lang::English en English forty-two

This list can be expanded! Contributions are welcomed.

Supported output types

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

  • .cardinal() (cardinal): forty-two (42)
  • .ordinal() (ordinal): forty-second (42)
  • .ordinal_num() (ordinal_num): 42nd (42)
  • .year() (year): nineteen oh-one (1901)
  • any currency: forty-two dollars and one cent (42.01)

Supported currencies

Here is a list of all of the supported currencies (with the ISO 4217 code):

  • Currency::AUD (AUD): australian dollar
  • Currency::CAD (CAD): canadian dollar
  • Currency::DOLLAR (DOLLAR): dollar
  • Currency::EUR (EUR): euro
  • Currency::GBP (GBP): pound
  • Currency::USD (USD): US dollar

About

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

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

You might also like...
hado-rshado β€” A little macro for writing haskell-like do expressions without too much ceremony

hado Monadic haskell-like expressions brought to rust via the hado! macro What? A little macro for writing haskell-like do expressions without too muc

Like jq, but for HTML. Uses CSS selectors to extract bits content from HTML files.

Like jq, but for HTML. Uses CSS selectors to extract bits content from HTML files. Mozilla's MDN has a good reference for CSS selector syntax.

Golang like WaitGroup implementation for sync/async Rust.

wg Golang like WaitGroup implementation for sync/async Rust.

The tool like Browserslist, but written in Rust.

browserslist-rs The tool like Browserslist, but written in Rust. Try it out Before trying this crate, you're required to get Rust installed. Then, clo

Freebsd-embedded-hal - Like linux-embedded-hal but FreeBSD

freebsd-embedded-hal Implementation of embedded-hal traits for FreeBSD devices: gpio: using libgpio, with stateful and toggleable support, with suppor

Stopwatch lib for rust. Start, pause, reset and lap like any stopwatch.

Chronometer Stopwatch lib for rust. Start, pause, reset and lap like any stopwatch. Nothing special I'm just learning rust. Getting Started Add this l

Booru software for the 21st century. (Name is supposed to be like Puro, the big monster, but I failed..)

Pooru Booru software for the 21st century. Setup Setup is a little funky, but I hope to fix this funkyness down the road. First and foremost, you will

A Raycast/Spotlight like app shell using tauri
A Raycast/Spotlight like app shell using tauri

Tauri Shell This repo can be used as reference for building alfred/raycast/spotlight apps using Tauri. Usage This reference repository is using Svelte

Proc. macro to generate C-like `enum` tags.

Continuous Integration Documentation Crates.io #[derive(EnumTag)] This crate provides a proc. macro to derive the EnumTag trait for the given Rust enu

Comments
  • Into<Number> for any number type

    Into for any number type

    For now, Into<Number> only allows i64 and f64.

    This has been chosen as this is the largest common number type that features the greatest range of numbers.

    However, people may work with other types and are fine using smaller number ranges.

    This forces the user to cast it to the 64 bits version of the number.

    This issue is a bit connected to #5 as if the int/float type is removed altogether with another it may need to add Into case for each common number types.

    opened by Ballasi 0
  • Add a .prefer() function

    Add a .prefer() function

    Something I would like to add for the English language before moving on to other languages is the ability to set preferences.

    For instance, here is a sample code:

    use num2words::*;
    
    fn print_game_score(home_score: i64, away_score: i64) -> Result<(), Num2Err> {
        println!(
            "{} - {}",
            Num2Words::new(home_score).to_words()?,
            Num2Words::new(away_score).to_words()?
        );
        Ok(())
    }
    

    Calling print_game_score(3, 0).unwrap() would print three - zero, but in this case, we are more likely to be interested to see three - nil, same for "oh" (e.g., in versioning).

    I am interested in implementing a call similar to the one below:

    Num2Words::new(home_score).prefer(["nil"]).to_words()
    
    opened by Ballasi 0
  • Add default currency name

    Add default currency name

    For use in the future, languages should be able to batch some "default string" for each currencies. If we need to add a new currency, we may not know its translation and use in various languages, this helps having a first implementation that is likely to work well in the first place and might be patched in the future if needed to. In a way, the currencies() function may work in a "override currency name" way.

    opened by Ballasi 0
Releases(v0.4.1)
  • v0.4.1(Oct 25, 2022)

    What's new?

    This updated version adds minor improvements

    • Adding many From<> traits to Num so that it can be created with something other than i64 and f64.
    • Minor code improvements and bug fixes
    • Preparation for big-num implementation

    Full change log

    Here is the full change log.

    Source code(tar.gz)
    Source code(zip)
    num2words(3.36 MB)
  • v0.4.0(Mar 31, 2022)

    What's new?

    • Addition of languages preferences

    Example:

    use num2words::*;
    
    fn print_game_score(home_score: i64, away_score: i64) -> Result<(), Num2Err> {
        println!(
            "{} - {}",
            Num2Words::new(home_score).prefer("nil").to_words()?,
            Num2Words::new(away_score).prefer("nil").to_words()?
        );
        Ok(())
    }
    
    • Add default currency names and ability to have different ways to write cents
    • Binary now shows the version of the program in words

    Full change log

    Here is the full change log.

    Source code(tar.gz)
    Source code(zip)
    num2words(3.36 MB)
  • v0.3.0(Mar 27, 2022)

    What's new?

    • Usage of specific functions rather than .output(). This makes the whole code easier to read and build.

    Example:

    use num2words::{Num2Words, Output};
    assert_eq!(
        Num2Words::new(42).output(Output::Ordinal).to_words(),
        Ok(String::from("forty-second"))
    );
    

    becomes

    use num2words::Num2Words;
    assert_eq!(
        Num2Words::new(42).ordinal().to_words(),
        Ok(String::from("forty-second"))
    );
    
    • Add Display trait to Num2Err as this may be useful for any usages (e.g., the binary provided with the library)
    • Update the binary to the new code structure
    • Enable the binary to output to stderr when providing an error

    Full changelog

    Here is the full changelog.

    Source code(tar.gz)
    Source code(zip)
    num2words(3.35 MB)
  • v0.2.0(Mar 27, 2022)

    What's new?

    • Usage of builder pattern rather than macros

    Examples:

    use num2words::*;
    assert_eq!(
        Num2Words::new(42).lang(Lang::English).to_words(),
        Ok(String::from("forty-two"))
    );
    assert_eq!(
        Num2Words::new(42).output(Output::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"))
    );
    
    • Update of call to $ num2words --help
    • Addition of call $ num2words --version
    • Complete overhaul of the docs and the README.md file

    Full changelog

    Here is the full changelog.

    Source code(tar.gz)
    Source code(zip)
    num2words(3.34 MB)
  • v0.1.0(Mar 26, 2022)

Owner
Asperatus
Free software consultant
Asperatus
A Rust library for random number generation.

A Rust library for random number generation.

null 1.3k Jan 6, 2023
1️⃣ el lisp number uno - one lisp to rule them all πŸ†

luno el lisp number uno luno is the one lisp to rule them all. Still experimental, do not use it in production yet. goals embeddable small size simple

Eva Pace 3 Apr 25, 2022
Get a diff between two OpenAPI descriptions.

Get the difference between two OpenAPI descriptions.

Marc-Andre Giroux 25 Aug 22, 2022
Convert Hygea calendar to an iCal file to easily import it to Google Calendar (Rust version)

Hygea to iCal Goal Hygea provides a calendar via PDF and an application called Recycle. I just wanted to use an iCal file to import it in my calendar.

Guillaume Quittet 2 Oct 28, 2021
Emoji-printer - Utility to convert strings with emoji shortcodes to strings with the emoji unicode

Emoji Printer Intro Utility to convert strings with emoji shortcodes (:sushi:) to strings with the emoji unicode ( ?? ) Install cargo add emoji-printe

Kyle Scully 2 Dec 30, 2021
Count and convert between different indexing schemes on utf8 string slices

Str Indices Count and convert between different indexing schemes on utf8 string slices. The following schemes are currently supported: Chars (or "Unic

Nathan Vegdahl 11 Dec 25, 2022
Convert an MCU register description from the EDC format to the SVD format

edc2svd Convert an MCU register description from the EDC format to the SVD format EDC files are used to describe the special function registers of PIC

Stephan 4 Oct 9, 2021
Convert Juniper configurations to 'set-style'

JCC: Juniper Config Converter Convert Juniper configurations. Takes a Juniper configuration as displayed using show configuration and transforms it to

null 4 Sep 1, 2023
Adapters to convert between different writable APIs.

I/O adapters This crate provides adapters to compose writeable traits in the standard library. The following conversions are available: fmt::Write ->

Alex Saveau 16 Dec 21, 2023
A lightning fast version of tmux-fingers written in Rust, copy/pasting tmux like vimium/vimperator

tmux-thumbs A lightning fast version of tmux-fingers written in Rust for copy pasting with vimium/vimperator like hints. Usage Press ( prefix + Space

Ferran Basora 598 Jan 2, 2023