Accounting

Struct Accounting 

Source
pub struct Accounting { /* private fields */ }
Expand description

Format numbers as money values according to settings.

Accounting struct fields and default value.

FieldTypeDescriptionDefaultExample
symbolStringcurrency symbol$$
precisionusizecurrency precision (decimal places)02
thousandStringthousand separator,.
decimalStringdecimal separator.,
format_positiveStringformat string for positive values ({v} = value, {s} = symbol){s}{v}{s} {v}
format_negativeStringformat string for negative values-{s}{v}{s} ({v})
format_zeroStringformat string for zero values{s}{v}{s} –

Implementations§

Source§

impl Accounting

Source

pub fn new_from(symbol: &str, precision: usize) -> Self

Create Accounting from symbol、 precision and default settings.

Source

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");
Source

pub fn new( symbol: &str, precision: usize, thousand: &str, decimal: &str, format: &str, format_negative: &str, format_zero: &str, ) -> Self

Create Accounting

Source

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")
Source

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")
Source

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 $");
Source

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), "$ --");
Source

pub fn set_format_negative(&mut self, str: &str)

Sets the format string for negative values.

Source

pub fn set_format_zero(&mut self, str: &str)

Sets the format string for zero values.

Source

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.

Trait Implementations§

Source§

impl Default for Accounting

Source§

fn default() -> Self

Returns a “default“ Accounting.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.