pub struct Num2Words { /* private fields */ }Expand description
Builder for num2words
Implementations§
Source§impl Num2Words
impl Num2Words
Sourcepub fn new<T>(num: T) -> Self
pub fn new<T>(num: T) -> Self
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"))
);Sourcepub fn parse(num: &str) -> Option<Self>
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"))
);Sourcepub fn lang(self, lang: Lang) -> Self
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"))
);Sourcepub fn cardinal(self) -> Self
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"))
);Sourcepub fn ordinal(self) -> Self
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"))
);Sourcepub fn ordinal_num(self) -> Self
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"))
);Sourcepub fn year(self) -> Self
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"))
);Sourcepub fn currency(self, currency: Currency) -> Self
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"))
);Sourcepub fn prefer<T>(self, prefer: T) -> Self
pub fn prefer<T>(self, prefer: T) -> Self
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("пʼятдесятьма одним"))
);