Num2Words

Struct Num2Words 

Source
pub struct Num2Words { /* private fields */ }
Expand description

Builder for num2words

Implementations§

Source§

impl Num2Words

Source

pub fn new<T>(num: T) -> Self
where T: Into<BigFloat>,

Creates a new builder

Example:

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

pub fn parse(num: &str) -> Option<Self>

Creates a new builder from a string

Example:

use num2words::Num2Words;
assert_eq!(
    Num2Words::parse("42").unwrap().to_words(),
    Ok(String::from("forty-two"))
);
assert_eq!(
    Num2Words::parse("1e3").unwrap().to_words(),
    Ok(String::from("one thousand"))
);
Source

pub fn lang(self, lang: Lang) -> Self

Sets the language of the output

For all of the available languages, see Lang.

Example:

use num2words::{Num2Words, Lang};
assert_eq!(
    Num2Words::new(42).lang(Lang::English).to_words(),
    Ok(String::from("forty-two"))
);
Source

pub fn cardinal(self) -> Self

Sets the type of output to cardinal (forty-two)

Example:

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

pub fn ordinal(self) -> Self

Sets the type of output to ordinal (forty-second)

Example:

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

pub fn ordinal_num(self) -> Self

Sets the type of output to numbered ordinal (42nd)

Example:

use num2words::Num2Words;
assert_eq!(
    Num2Words::new(42).ordinal_num().to_words(),
    Ok(String::from("42nd"))
);
Source

pub fn year(self) -> Self

Sets the type of output to year (nineteen oh-one)

Example:

use num2words::Num2Words;
assert_eq!(
    Num2Words::new(1901).year().to_words(),
    Ok(String::from("nineteen oh-one"))
);
Source

pub fn currency(self, currency: Currency) -> Self

Sets the output to the currency it has been given

For all of the available currencies, see Currency.

Example:

use num2words::{Num2Words, Currency};
assert_eq!(
    Num2Words::new(42.01).currency(Currency::DOLLAR).to_words(),
    Ok(String::from("forty-two dollars and one cent"))
);
Source

pub fn prefer<T>(self, prefer: T) -> Self
where T: Into<String>,

Adds a preference parameter

§English language accepts:

oh and/or nil as replacements for “zero”

§French language accepts:

feminine/f/féminin/feminin

reformed/1990/rectifié/rectification

§Ukrainian language supports grammatical categories (bold - default):

Number: singular/sing/однина/од, plural/pl/множина/мн

Gender: masculine/m/чоловічий/чол/ч, feminine/f/жіночий/жін/ж, neuter/n/середній/сер/с

Declension: nominative/nom/називний/н, genitive/gen/родовий/р, dative/dat/давальний/д, accusative/acc/знахідний/з, instrumental/inc/орудний/о, locative/loc/місцевий/м

Examples:

use num2words::{Num2Words, Lang};
assert_eq!(
    Num2Words::new(0.05).prefer("oh").to_words(),
    Ok(String::from("point oh five"))
);
assert_eq!(
    Num2Words::new(161).lang(Lang::French).prefer("f").prefer("reformed").to_words(),
    Ok(String::from("cent-soixante-et-une"))
);
assert_eq!(
    Num2Words::new(51).lang(Lang::Ukrainian).prefer("орудний").to_words(),
    Ok(String::from("пʼятдесятьма одним"))
);
Source

pub fn to_words(self) -> Result<String, Num2Err>

Builds the output

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.