pub struct Currency { /* private fields */ }
Expand description
Represents currency through an optional symbol and amount of coin.
Every 100 coins represents a banknote. (coin: 100 => 1.00)
Implementations§
Source§impl Currency
impl Currency
Sourcepub fn from_str(s: &str) -> Result<Currency, ParseCurrencyError>
pub fn from_str(s: &str) -> Result<Currency, ParseCurrencyError>
Parses a string literal (&str) and attempts to convert it into a currency. Returns
Ok(Currency)
on a successful conversion, otherwise Err(ParseCurrencyError)
.
§Examples
use currency::Currency;
let c1 = Currency::from_str("$42.32").unwrap();
let c2 = Currency::from_str("$0.10").unwrap();
assert_eq!(c1 + c2, Currency::from_str("$42.42").unwrap());
Sourcepub fn value(&self) -> &BigInt
pub fn value(&self) -> &BigInt
Returns the number of coins held in the Currency
as &BigInt
.
Should you need ownership of the returned BigInt
, call clone()
on it.
§Examples
extern crate num;
extern crate currency;
fn main() {
use num::traits::ToPrimitive;
use currency::Currency;
let c1 = Currency::new();
assert_eq!(c1.value().to_u32().unwrap(), 0);
let c2 = Currency::from_str("$1.42").unwrap();
assert_eq!(c2.value().to_u32().unwrap(), 142);
}
Sourcepub fn convert(&self, conversion_rate: f64, currency_symbol: char) -> Currency
pub fn convert(&self, conversion_rate: f64, currency_symbol: char) -> Currency
Returns a new Currency
by multiplying the coin by the conversion rate and changing the
symbol.
§Examples
use currency::Currency;
let dollars = Currency::from_str("$10.00").unwrap();
let conv_rate = 0.89;
let euros = dollars.convert(0.89, '€');
assert_eq!(euros, Currency::from_str("€8.90").unwrap());
Trait Implementations§
Source§impl Display for Currency
Allows any Currency to be displayed as a String. The format includes comma delimiting with a
two digit precision decimal.
impl Display for Currency
Allows any Currency to be displayed as a String. The format includes comma delimiting with a two digit precision decimal.
§Example
use currency::Currency;
let dollars = Currency::from_str("$12.10").unwrap();
assert_eq!(dollars.to_string(), "$12.10");
let euros = Currency::from_str("£1.000").unwrap();
assert_eq!(format!("{:e}", euros), "£1.000,00");
Source§impl<'a, 'b> Div<&'b Currency> for &'a Currency
Overloads the ‘/’ operator between two borrowed Currency objects.
impl<'a, 'b> Div<&'b Currency> for &'a Currency
Overloads the ‘/’ operator between two borrowed Currency objects.
§Panics
Panics if they aren’t the same type of currency, as denoted by the currency’s symbol.
Source§impl<'a> Div<&'a Currency> for Currency
Overloads the ‘/’ operator between an owned Currency object and a borrowed one.
impl<'a> Div<&'a Currency> for Currency
Overloads the ‘/’ operator between an owned Currency object and a borrowed one.
§Panics
Panics if they aren’t the same type of currency, as denoted by the currency’s symbol.
Source§impl<'a> Div<Currency> for &'a Currency
Overloads the ‘/’ operator between a borrowed Currency object and an owned one.
impl<'a> Div<Currency> for &'a Currency
Overloads the ‘/’ operator between a borrowed Currency object and an owned one.
§Panics
Panics if they aren’t the same type of currency, as denoted by the currency’s symbol.
Source§impl Div for Currency
Overloads the ‘/’ operator between two owned Currency objects.
impl Div for Currency
Overloads the ‘/’ operator between two owned Currency objects.
§Panics
Panics if they aren’t the same type of currency, as denoted by the currency’s symbol.
Source§impl LowerExp for Currency
Identical to the implementation of Display, but replaces the “.” with a “,”. Access this
formatting by using “{:e}”.
impl LowerExp for Currency
Identical to the implementation of Display, but replaces the “.” with a “,”. Access this formatting by using “{:e}”.
§Example
use currency::Currency;
let euros = Currency::from_str("£1000,99").unwrap();
println!("{:e}", euros);
Which prints:
"£1.000,99"