cashmoney

Crates.iocashmoney
lib.rscashmoney
version
sourcesrc
created_at2024-12-13 00:02:06.330752
updated_at2024-12-13 00:02:06.330752
descriptionLibrary for safe monetary calculations
homepagehttps://github.com/arcanyx-pub/cashmoney-rs
repositoryhttps://github.com/arcanyx-pub/cashmoney-rs
max_upload_size
id1481756
Cargo.toml error:TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Joe Dahlquist (jdahlq)

documentation

https://docs.rs/cashmoney

README

🤑 Cashmoney 💸

Crates.io Documentation Crates.io

Cashmoney is a Rust library for expressing monetary values and performing safe monetary calculations suitable for financial applications.

Features

  • Ensure monetary values have the correct number of decimal places according to their currency.
  • Prevent accidental summation of disparate currencies.
  • Use exact or high-precision representation for intermediate calculations.
  • Round monetary values as the final step in a series of calculations by an explicit API call.

Usage

Creating a Money object

let value = dec!(13.37);
let a: Result<Money> = Money::from(value, Currency::USD);

assert_eq!(a, b);

// Convenience macros. Note that these macros rely on the rust_decimal crate's
// `dec!` macro to parse the values.
let c = cad!(13.37;
let d = usd!(13.37);

Adding and subtracting

let sum: Money = usd!(13) + usd!(0.37);
assert_eq!(sum, usd!(13.37));

// Returns Err:
let _ = usd!(13) + cad!(0.37);

let difference: Money = usd!(14.00) - usd!(0.63);
assert_eq!(difference, usd!(13.37));

Scalar multiplication and division

// FractionalMoney represents a monetary value that may be a more precise value
// than the smallest denomination for the given currency. For example, the
// value below would be USD $6.685.
let product: FractionalMoney = usd!(13.37) * dec!(0.5);
assert_eq!(product.fractional_value(), dec!(6.685));

// "Standard" rounding (midpoint rounds away from zero)
let rounded_up: Money = product.round_up();
assert_eq!(rounded_up, usd!(6.69));

// "Banker's rounding"
let rounded: Money = product.round();
assert_eq!(rounded, usd!(6.68));
Commit count: 13

cargo fmt