Simple money representation.
The Money
type represents dollars
and cents
as separate values to ensure precision.
Additionally, it tracks whether the amount is positive or negative with a boolean member.
The library supports basic mathematical and relational operations on Money
types.
A string representation of the Money
type is available with .to_string()
.
The arguments to Money::new()
are:
1) dollars (unsigned integer)
2) cents (unsigned integer)
3) positive (boolean)
dollars
and cents
are absolute values. Whether the entire value is positive or negative is determined by the positive
argument.
```
let m1 = Money::new(5, 25, true).unwrap(); // default string representation: "$5.25" let m2 = Money::new(12, 96, false).unwrap(); // default string representation: "-$12.96" ```
The library supports some basic customization with regard to the string received from the Money
type.
As the Money
type was designed around American money representation, the default symbol is the dollar sign ('$'). However, this can be changed via options:
```
let mut m = Money::new(5, 25, true).unwrap(); m.options().set_symbol('£');
asserteq!(m.tostring(), "£5.25"); ```
The symbol is shown by default. If the symbol is not needed in the string representation, it can be suppressed:
```
let mut m = Money::new(5, 25, true).unwrap(); m.options().setshowsymbol(false);
asserteq!(m.tostring(), "5.25"); ```
Negative amounts are indicated with a minus sign by default. The options are: * Minus (default) * Parenthesis * Hide (negation is not indicated)
```
let mut m = Money::new(5, 25, false).unwrap(); m.options().setnegativeview(NegativeView::Minus); // default setting
asserteq!(m.tostring(), "-$5.25"); ```
```
let mut m = Money::new(5, 25, false).unwrap(); m.options().setnegativeview(NegativeView::Paren);
asserteq!(m.tostring(), "($5.25)"); ```
```
let mut m = Money::new(5, 25, false).unwrap(); m.options().setnegativeview(NegativeView::Hide);
asserteq!(m.tostring(), "$5.25"); ```
nmoney
uses the MIT license.