Struct currency::Currency
[−]
[src]
pub struct Currency { // some fields omitted }
Represents currency through an optional symbol and amount of coin.
Every 100 coins represents a banknote. (coin: 100 => 1.00)
Methods
impl Currency
fn new() -> Self
Creates a blank Currency with no symbol and 0 coin.
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());
fn sign(&self) -> Sign
Returns the Sign
of the BigInt
holding the coins.
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); }
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
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");
impl FromStr for Currency
type Err = ParseCurrencyError
fn from_str(s: &str) -> Result<Currency, ParseCurrencyError>
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:
text "£1.000,99"
impl<'a, 'b> Add<&'b Currency> for &'a Currency
impl<'a> Add<Currency> for &'a Currency
impl<'a> Add<&'a Currency> for Currency
impl Add<Currency> for Currency
impl<'a, 'b> Sub<&'b Currency> for &'a Currency
impl<'a> Sub<Currency> for &'a Currency
impl<'a> Sub<&'a Currency> for Currency
impl Sub<Currency> for Currency
impl<'a, 'b> Mul<&'b BigUint> for &'a Currency
impl<'a> Mul<BigUint> for &'a Currency
impl<'a> Mul<&'a BigUint> for Currency
impl Mul<BigUint> for Currency
impl<'a, 'b> Mul<&'b u8> for &'a Currency
impl<'a> Mul<u8> for &'a Currency
impl<'a> Mul<&'a u8> for Currency
impl Mul<u8> for Currency
impl<'a, 'b> Mul<&'b u16> for &'a Currency
impl<'a> Mul<u16> for &'a Currency
impl<'a> Mul<&'a u16> for Currency
impl Mul<u16> for Currency
impl<'a, 'b> Mul<&'b u32> for &'a Currency
impl<'a> Mul<u32> for &'a Currency
impl<'a> Mul<&'a u32> for Currency
impl Mul<u32> for Currency
impl<'a, 'b> Mul<&'b u64> for &'a Currency
impl<'a> Mul<u64> for &'a Currency
impl<'a> Mul<&'a u64> for Currency
impl Mul<u64> for Currency
impl<'a, 'b> Mul<&'b usize> for &'a Currency
impl<'a> Mul<usize> for &'a Currency
impl<'a> Mul<&'a usize> for Currency
impl Mul<usize> for Currency
impl<'a, 'b> Mul<&'b i8> for &'a Currency
impl<'a> Mul<i8> for &'a Currency
impl<'a> Mul<&'a i8> for Currency
impl Mul<i8> for Currency
impl<'a, 'b> Mul<&'b i16> for &'a Currency
impl<'a> Mul<i16> for &'a Currency
impl<'a> Mul<&'a i16> for Currency
impl Mul<i16> for Currency
impl<'a, 'b> Mul<&'b i32> for &'a Currency
impl<'a> Mul<i32> for &'a Currency
impl<'a> Mul<&'a i32> for Currency
impl Mul<i32> for Currency
impl<'a, 'b> Mul<&'b i64> for &'a Currency
impl<'a> Mul<i64> for &'a Currency
impl<'a> Mul<&'a i64> for Currency
impl Mul<i64> for Currency
impl<'a, 'b> Mul<&'b isize> for &'a Currency
impl<'a> Mul<isize> for &'a Currency
impl<'a> Mul<&'a isize> for Currency
impl Mul<isize> for Currency
impl<'a, 'b> Div<&'b BigUint> for &'a Currency
impl<'a> Div<BigUint> for &'a Currency
impl<'a> Div<&'a BigUint> for Currency
impl Div<BigUint> for Currency
impl<'a, 'b> Div<&'b u8> for &'a Currency
impl<'a> Div<u8> for &'a Currency
impl<'a> Div<&'a u8> for Currency
impl Div<u8> for Currency
impl<'a, 'b> Div<&'b u16> for &'a Currency
impl<'a> Div<u16> for &'a Currency
impl<'a> Div<&'a u16> for Currency
impl Div<u16> for Currency
impl<'a, 'b> Div<&'b u32> for &'a Currency
impl<'a> Div<u32> for &'a Currency
impl<'a> Div<&'a u32> for Currency
impl Div<u32> for Currency
impl<'a, 'b> Div<&'b u64> for &'a Currency
impl<'a> Div<u64> for &'a Currency
impl<'a> Div<&'a u64> for Currency
impl Div<u64> for Currency
impl<'a, 'b> Div<&'b usize> for &'a Currency
impl<'a> Div<usize> for &'a Currency
impl<'a> Div<&'a usize> for Currency
impl Div<usize> for Currency
impl<'a, 'b> Div<&'b i8> for &'a Currency
impl<'a> Div<i8> for &'a Currency
impl<'a> Div<&'a i8> for Currency
impl Div<i8> for Currency
impl<'a, 'b> Div<&'b i16> for &'a Currency
impl<'a> Div<i16> for &'a Currency
impl<'a> Div<&'a i16> for Currency
impl Div<i16> for Currency
impl<'a, 'b> Div<&'b i32> for &'a Currency
impl<'a> Div<i32> for &'a Currency
impl<'a> Div<&'a i32> for Currency
impl Div<i32> for Currency
impl<'a, 'b> Div<&'b i64> for &'a Currency
impl<'a> Div<i64> for &'a Currency
impl<'a> Div<&'a i64> for Currency
impl Div<i64> for Currency
impl<'a, 'b> Div<&'b isize> for &'a Currency
impl<'a> Div<isize> for &'a Currency
impl<'a> Div<&'a isize> for Currency
impl Div<isize> for Currency
impl<'a, 'b> Mul<&'b f32> for &'a Currency
impl<'a> Mul<f32> for &'a Currency
impl<'a> Mul<&'a f32> for Currency
impl Mul<f32> for Currency
impl<'a, 'b> Mul<&'b f64> for &'a Currency
impl<'a> Mul<f64> for &'a Currency
impl<'a> Mul<&'a f64> for Currency
impl Mul<f64> for Currency
impl<'a, 'b> Div<&'b f32> for &'a Currency
impl<'a> Div<f32> for &'a Currency
impl<'a> Div<&'a f32> for Currency
impl Div<f32> for Currency
impl<'a, 'b> Div<&'b f64> for &'a Currency
impl<'a> Div<f64> for &'a Currency
impl<'a> Div<&'a f64> for Currency
impl Div<f64> for Currency
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.
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.
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.
impl Div<Currency> 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.