7 unstable releases (3 breaking)

Uses old Rust 2015

0.4.0 Apr 24, 2016
0.3.0 Dec 17, 2015
0.2.0 Aug 4, 2015
0.1.3 Jul 24, 2015
0.1.2 Jun 2, 2015

#266 in Value formatting

Download history 26/week @ 2023-10-28 17/week @ 2023-11-04 19/week @ 2023-11-11 15/week @ 2023-11-18 28/week @ 2023-11-25 21/week @ 2023-12-02 13/week @ 2023-12-09 21/week @ 2023-12-16 23/week @ 2023-12-23 9/week @ 2023-12-30 25/week @ 2024-01-06 13/week @ 2024-01-13 15/week @ 2024-01-20 28/week @ 2024-01-27 13/week @ 2024-02-03 31/week @ 2024-02-10

89 downloads per month

MIT license

36KB
727 lines

Rust Currency Library

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

Documentation.

Crate.

Progress

I am slowly but surely working on an implementation of an IEEE standard decimal encoding in another repo.

Once that is finished, this currency library will be refactored to use the MUCH smaller encoding, allowing the type to be cloned and copied freely.

Contributing

See CONTRIBUTING.md (https://github.com/Tahler/currency-rs/blob/master/CONTRIBUTING.md) if you'd like to contribute to this project.


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

~245KB