#currency #international #money #iso #bank

qsv_currency

A library for representing international currencies

3 releases (breaking)

0.7.0 Sep 21, 2024
0.6.0 Dec 18, 2022
0.5.0 Dec 28, 2021

#63 in Finance

Download history 244/week @ 2024-07-29 263/week @ 2024-08-05 229/week @ 2024-08-12 351/week @ 2024-08-19 147/week @ 2024-08-26 243/week @ 2024-09-02 232/week @ 2024-09-09 308/week @ 2024-09-16 373/week @ 2024-09-23 213/week @ 2024-09-30 296/week @ 2024-10-07 232/week @ 2024-10-14 335/week @ 2024-10-21 266/week @ 2024-10-28 231/week @ 2024-11-04 222/week @ 2024-11-11

1,087 downloads per month
Used in qsv

MIT license

51KB
1K SLoC

Rust Currency Library

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

This is a fork of https://github.com/Tahler/currency-rs, pulling in pending PRs for currency strings (e.g. "USD, EUR, etc." - not just single character symbols - "$, €, etc.") and serde support.

It also upgrades the num dependency from 0.1.32 to 0.4.0 and adds a is_iso_currency function to check if a currency is an ISO currency.

This fork was primarily created for the qsv CSV data-wrangling toolkit.


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 qsv_currency;

fn main() {
    use qsv_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

~1.6–2.4MB
~46K SLoC