Crates.io | accounting |
lib.rs | accounting |
version | 0.2.0 |
source | src |
created_at | 2022-01-25 19:14:53.005874 |
updated_at | 2022-02-20 18:34:51.031909 |
description | accounting is a library for money and currency formatting. |
homepage | |
repository | https://github.com/silvonli/accounting |
max_upload_size | |
id | 521027 |
size | 47,884 |
accounting is a library for money and currency formatting. (inspired by accounting for golang)
use accounting::Accounting;
fn main() {
let mut ac = Accounting::new_from("$", 2);
ac.set_format("{s} {v}");
assert_eq!(ac.format_money(1000000), "$ 1,000,000.00");
assert_eq!(ac.format_money(-5000), "-$ 5,000.00");
}
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.
#[cfg(feature="decimal")]
fn format_decimal_type() {
let mut ac = Accounting::new_from("$", 2);
ac.set_format("{s} {v}");
let x = rust_decimal::Decimal::new(-12345678921, 2);
assert_eq!(ac.format_money(x), "-$ 123,456,789.21");
}
If you want use decimal numbers,enable feature decimal
,than you can use decimal number supported by rust_decimal lib. like above.
pub struct Accounting {
symbol: String,
precision: usize,
thousand: String,
decimal: String,
format_positive: String,
format_negative: String,
format_zero: String
}
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} -- |
let mut ac = Accounting::new_from("$", 2);
ac.set_format_positive("{s} {v}");
ac.set_format_negative("{s} ({v})");
ac.set_format_zero( "{s} --");
assert_eq!(ac.format_money(1000000), "$ 1,000,000.00");
assert_eq!(ac.format_money(-5000), "$ (5,000.00)");
assert_eq!(ac.format_money(0), "$ --");
let mut ac = Accounting::new_from("$", 2);
ac.set_thousand_separator("'");
assert_eq!(ac.format_money(123456789.213123), "$123'456'789.21")
let mut ac = Accounting::new_from("$", 2);
ac.set_decimal_separator("'");
assert_eq!(ac.format_money(123456789.213123), "$123,456,789'21")
format_money
function parameter need to implement 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:
rust_decimal::Decimal
Trait define:pub trait FormatNumber {
fn format_number(&self, precision: usize, thousand: &str, decimal: &str) -> String;
}
Examples:
use accounting::FormatNumber;
let x = 123456789.213123f64;
assert_eq!(x.format_number(2, ",", "."), "123,456,789.21");
unformat
function strips out all currency formatting and returns the numberic string.
Examples:
use accounting::unformat;
assert_eq!(unformat("$4,500.23", 2, "USD"), Ok("4500.23".to_string()));
assert_eq!(unformat("EUR 12.500,3474", 3, "EUR"), Ok("12500.347".to_string()));