#currency #money #currencies #convert #optional #representing #fork

yanked ramn-currency

A library for representing currencies. Fork of https://github.com/Tahler/currency-rs

Uses old Rust 2015

0.4.1 Jan 16, 2017
0.4.0 Jan 7, 2017

#4 in #representing

MIT license

36KB
730 lines

Rust Currency Library

A very small library, providing a way to represent currencies in Rust.

Documentation.

Crate.


lib.rs:

A Currency is a combination of an optional character (Option<char>``) and a big integer (BigInt`).

Common operations are overloaded to make numerical operations easy.

Perhaps the most useful part of this crate is the Currency::from_str function, which can convert international currency representations such as "$1,000.42" and "£10,99" into a usable Currency instance.

Example

extern crate currency;

fn main() {
    use currency::Currency;

    let sock_price = Currency::from_str("$11.99").unwrap();
    let toothbrush_price = Currency::from_str("$1.99").unwrap();
    let subtotal = sock_price + toothbrush_price;
    let tax_rate = 0.07;
    let total = &subtotal + (&subtotal * tax_rate);
    assert_eq!(format!("{}", total), "$14.95");
}

Limitations

This crate cannot lookup conversion data dynamically. It does supply a convert function, but the conversion rates will need to be input by the user.

This crate also does not handle rounding or precision. Values are truncated during multiplication, division, and extra precision in a parse (such as gas prices).

Dependencies

~240KB