Expand description
Quotation / MoneyValue (units + nano) <-> [Decimal], exactly.
T-Bank prices are units (integer part) and nano (billionths); both carry the sign of the value.
A [Decimal] holds every such value without loss, so the conversion is exact in both directions.
There is no f64 on the way. Going back needs at most 9 decimal places: a value with more is
refused with MoneyError::TooManyDecimals and never rounded in silence.
The trailing zeros are dropped on the way in (Quotation { units: 114, nano: 250_000_000 } is
114.25). A [Decimal] compares by value, so the scale does not matter.
§Examples
use rust_decimal::Decimal;
use brokapi_tbank_protocol::money::{
decimal_to_money, decimal_to_parts, decimal_to_quotation, money_to_decimal, quotation_to_decimal,
};
// 114.25 is 114 units and 250 000 000 nano.
let price = Decimal::new(11425, 2);
let q = decimal_to_quotation(price)?;
assert_eq!((q.units, q.nano), (114, 250_000_000));
assert_eq!(quotation_to_decimal(&q), price);
// A negative value carries the sign on BOTH parts.
assert_eq!(decimal_to_parts(Decimal::new(-2002, 1))?, (-200, -200_000_000));
// Money keeps the currency code next to the amount.
let m = decimal_to_money("rub", Decimal::new(15005, 1))?;
assert_eq!((m.currency.as_str(), m.units, m.nano), ("rub", 1500, 500_000_000));
assert_eq!(money_to_decimal(&m), Decimal::new(15005, 1));Enums§
- Money
Error - Why a number could not be turned into
unitsandnano.
Functions§
- decimal_
to_ money - Builds a
MoneyValuewith the given currency code from a number. - decimal_
to_ parts - Splits a number into
(units, nano). Both parts get the sign of the value. - decimal_
to_ quotation - Builds a
Quotationfrom a number. - money
money_from_strfor a literal that is known to be right (tests, examples, imitations).- money_
from_ str - Parses a decimal text into a
MoneyValueof the given currency. - money_
to_ decimal - Converts a
MoneyValueto a [Decimal], ignoring its currency. - parts_
to_ decimal - Converts raw
unitsandnanoto a [Decimal], exactly. - quotation
quotation_from_strfor a literal that is known to be right (tests, examples, imitations).- quotation_
from_ str - Parses a decimal text into a
Quotation:"273.45". - quotation_
to_ decimal - Converts a
Quotation(a price or a quantity in pieces) to a [Decimal], exactly.