Skip to main content

Module money

Module money 

Source
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§

MoneyError
Why a number could not be turned into units and nano.

Functions§

decimal_to_money
Builds a MoneyValue with 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 Quotation from a number.
money
money_from_str for a literal that is known to be right (tests, examples, imitations).
money_from_str
Parses a decimal text into a MoneyValue of the given currency.
money_to_decimal
Converts a MoneyValue to a [Decimal], ignoring its currency.
parts_to_decimal
Converts raw units and nano to a [Decimal], exactly.
quotation
quotation_from_str for 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.