Crate steel_cent [−] [src]
Currency and money values for Rust, plus customizable formatting of money values and reference data for real-world currencies.
Examples
use steel_cent::Money; use steel_cent::currency::USD; use steel_cent::formatting::{us_style, uk_style, france_style}; let price = Money::of_major_minor(USD, 19, 95); let shipping_and_handling = Money::of_major(USD, 10); let convenience_charge = Money::of_major(USD, 6); let fees = shipping_and_handling + convenience_charge; let discount: f64 = 1.0 - 0.2; // 20% off let discounted_price = price * discount; let total = discounted_price + fees; assert_eq!(Money::of_major_minor(USD, 15, 96), discounted_price); assert_eq!(Money::of_major_minor(USD, 31, 96), total); assert_eq!((price * discount) + shipping_and_handling + convenience_charge, total); assert_eq!("total: $31.96", format!("total: {}", us_style().display_for(&total))); assert_eq!("total: USD31.96", format!("total: {}", uk_style().display_for(&total))); assert_eq!("total: 31,96\u{a0}USD", format!("total: {}", france_style().display_for(&total)));
Money and SmallMoney
The crate provides two representations of an amount of money in a certain currency, both of which have similar implementations and feature similar behavior. They internally represent their amounts as an integer of the "minor" unit of their currency. For example a US-Dollar amount is stored as an integer number of cents.
The starting point for most uses should probably be Money
. The range of values it can
represent should be large enough for almost all uses.
SmallMoney
is only 64 bits in size, which might be nice from a performance perspective, but
the range of values it can represent is quite limited. See the doc tests of min
and max
for details.
Reexports
pub use currency::Currency; |
Modules
currency |
Besides defining the |
formatting |
Support for formatting and parsing of monetary values. |
Structs
Money |
A signed amount of money in a certain currency, with the currency's standard number of decimal places. |
SmallMoney |
A signed amount of money in a certain currency, with the currency's standard number of decimal places. |