pub struct Accounting { /* private fields */ }Expand description
Format numbers as money values according to settings.
Accounting struct fields and default value.
| 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} – |
Implementations§
Source§impl Accounting
impl Accounting
Sourcepub fn new_from(symbol: &str, precision: usize) -> Self
pub fn new_from(symbol: &str, precision: usize) -> Self
Create Accounting from symbol、 precision and default settings.
Sourcepub fn new_from_seperator(
symbol: &str,
precision: usize,
thousand: &str,
decimal: &str,
) -> Self
pub fn new_from_seperator( symbol: &str, precision: usize, thousand: &str, decimal: &str, ) -> Self
Create Accounting from symbol、 precision、thousand separator、 decimal separator and default settings.
§Examples
let ac = Accounting::new_from_seperator("€", 2, ".", ",");
assert_eq!(ac.format_money(4999.99), "€4.999,99");Sourcepub fn new(
symbol: &str,
precision: usize,
thousand: &str,
decimal: &str,
format: &str,
format_negative: &str,
format_zero: &str,
) -> Self
pub fn new( symbol: &str, precision: usize, thousand: &str, decimal: &str, format: &str, format_negative: &str, format_zero: &str, ) -> Self
Create Accounting
Sourcepub fn set_thousand_separator(&mut self, str: &str)
pub fn set_thousand_separator(&mut self, str: &str)
Sets the separator for the thousands separation.
§Examples
let mut ac = Accounting::new_from("$", 2);
ac.set_thousand_separator("'");
assert_eq!(ac.format_money(123456789.213123), "$123'456'789.21")Sourcepub fn set_decimal_separator(&mut self, str: &str)
pub fn set_decimal_separator(&mut self, str: &str)
Sets the separator for the decimal separation.
§Examples
let mut ac = Accounting::new_from("$", 2);
ac.set_decimal_separator("'");
assert_eq!(ac.format_money(123456789.213123), "$123,456,789'21")Sourcepub fn set_format(&mut self, str: &str)
pub fn set_format(&mut self, str: &str)
Sets the format string for positive and zero value.
Also Sets format string by adding - at begining for negative value.
§Examples
let mut ac = Accounting::new_from("$", 2);
ac.set_format("{v} {s}");
assert_eq!(ac.format_money(123456789.213123), "123,456,789.21 $");
assert_eq!(ac.format_money(-123456789.213123), "-123,456,789.21 $");
assert_eq!(ac.format_money(0), "0.00 $");Sourcepub fn set_format_positive(&mut self, str: &str)
pub fn set_format_positive(&mut self, str: &str)
Sets the format string for positive values.
§Examples
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), "$ --");Sourcepub fn set_format_negative(&mut self, str: &str)
pub fn set_format_negative(&mut self, str: &str)
Sets the format string for negative values.
Sourcepub fn set_format_zero(&mut self, str: &str)
pub fn set_format_zero(&mut self, str: &str)
Sets the format string for zero values.
Sourcepub fn format_money<T: FormatNumber>(&self, value: T) -> String
pub fn format_money<T: FormatNumber>(&self, value: T) -> String
format_money function format numbers as money values,
using customisable settings of currency symbol, precision, and thousand/decimal separators.
The value type need to implement FormatNumber trait.