accounting
accounting is a library for money and currency formatting. (inspired by accounting for golang)
Examples:
use Accounting;
Set the format string of Accounting variable,then format numbers as money values. In the format string:
- {v} is placehoder of value, will be replaced by number.
- {s} is placehoder of symbol, will be replaced by currency symbol like $、¥ and so on.
If you want use decimal numbers,enable feature decimal,than you can use decimal number supported by rust_decimal lib. like above.
Accounting struct
| Field | Type | Description | Default | Example |
|---|---|---|---|---|
| symbol | String | currency symbol | $ | $ |
| precision | usize | currency precision (decimal places) | 0 | 2 |
| thousand | String | thousand separator | , | . |
| decimal | String | decimal separator | . | , |
| format_positive | String | format string for positive values ({v} = value, {s} = symbol) | {s}{v} | {s} {v} |
| format_negative | String | format string for negative values | -{s}{v} | {s} ({v}) |
| format_zero | String | format string for zero values | {s}{v} | {s} -- |
Examples:
- Set format string.
let mut ac = new_from;
ac.set_format_positive;
ac.set_format_negative;
ac.set_format_zero;
assert_eq!;
assert_eq!;
assert_eq!;
- Set thousand separator.
let mut ac = new_from;
ac.set_thousand_separator;
assert_eq!
- Set decimal separator.
let mut ac = new_from;
ac.set_decimal_separator;
assert_eq!
format_money function parameter need to implement FormatNumber trait.
FormatNumber trait
FormatNumber is a trait of the library.
The type which implement this trait can be format to string with custom precision and separators. Implemented types include:
- primitive type: i8, u8, i16, u16 i32, u32 i64, u64, i128, u128, isize, usize, f32, f64.
- decimal type:
rust_decimal::DecimalTrait define:
Examples:
use FormatNumber;
let x = 123456789.213123f64;
assert_eq!;
unformat function
unformat function strips out all currency formatting and returns the numberic string.
Examples:
use unformat;
assert_eq!;
assert_eq!;