greenback

Crates.iogreenback
lib.rsgreenback
version0.0.3
sourcesrc
created_at2017-11-26 07:30:01.925608
updated_at2017-12-02 06:14:32.536943
descriptionLibrary for safely handling USD values as integers
homepagehttps://github.com/dseevr/greenback
repositoryhttps://github.com/dseevr/greenback
max_upload_size
id40541
size14,194
Bill Robinson (dseevr)

documentation

https://docs.rs/greenback/0.0.3/greenback/

README

greenback on crates.io Build Status

greenback

Library for safely handling USD values as integers

Installation

Edit Cargo.toml:

[dependencies]
greenback="0.0.3"

Usage

extern crate greenback;
use greenback::Greenback;

fn main() {

    // regular arithmetic operations

    let unit_price = Greenback::new(10, 99); // $10.99
    let quantity = 10;
    let coupon = Greenback::new(2, 0); // $2.00 off

    let total_cost = unit_price * quantity - coupon;

    println!("Total cost: {}", total_cost);

    // summation example

    let foo = Greenback::from_float(1.23); // $1.23
    let bar = Greenback::from_cents(4_56); // $4.56
    let baz = Greenback::new(3, 50);       // $3.50

    let sum: Greenback = vec![foo, bar, baz].into_iter().sum();

    if sum > Greenback::zero() {
        println!("sum: {}", sum);
    }
}

Output:

Total cost: $107.90
sum: $9.29

Status

This is my first Rust package and I'm still learning Rust so user beware!

Pull requests and issues are very welcome.

All the basic arithmetic operations (Add, AddMul, Sub, SubMul, etc.) are implemented along with ordering and summing. There's also a default formatter which will print your money values like $1,234.56. There's no overflow detection and only limited support for Mul and Div traits. I need to figure out how to do these using generics properly.

Commit count: 16

cargo fmt