pub struct Decimal<const K: u32>(/* private fields */);Expand description
A non-negative fixed-point decimal cost with K decimal places.
Stores an integer number of units of 10^-K in a u64, so Decimal::<2> counts
hundredths. Arithmetic is exact integer arithmetic: sums never round and are independent
of the summation order, which floats cannot promise. The scale is part of the type, so
costs of different scales cannot be mixed by accident.
K ranges from 0 to 19; a larger K is rejected at compile time as soon as a
constructor, Decimal::SCALE or formatting is used. The largest representable cost is
u64::MAX / 10^K whole units plus the fraction; + overflows like u64 (see the
Cost contract).
// 10^20 does not fit into u64.
let _ = search_core::Decimal::<20>::from_raw(1);Default is zero and, being derived, skips that check; it is harmless because every
operation that needs the scale performs it.
Display and Debug both print the decimal value with
exactly K fraction digits; FromStr parses it back exactly,
rejecting text that would round (see ParseDecimalError).
§Examples
use search_core::{Cost, Decimal};
type Cents = Decimal<2>;
let step = Cents::from_raw(125); // 1.25
let bonus = Cents::from_int(2).unwrap();
assert_eq!(step + bonus, Cents::from_raw(325));
assert_eq!((step + bonus).to_string(), "3.25");
assert!(Cents::ZERO < step);Implementations§
Source§impl<const K: u32> Decimal<K>
impl<const K: u32> Decimal<K>
Trait Implementations§
impl<const K: u32> Copy for Decimal<K>
Source§impl<const K: u32> Cost for Decimal<K>
impl<const K: u32> Cost for Decimal<K>
Source§fn saturating_add(self, other: Self) -> Self
fn saturating_add(self, other: Self) -> Self
Source§fn saturating_times(self, factor: u32) -> Self
fn saturating_times(self, factor: u32) -> Self
Source§impl<const K: u32> CostExt for Decimal<K>
impl<const K: u32> CostExt for Decimal<K>
impl<const K: u32> Eq for Decimal<K>
Source§impl<const K: u32> ExactArith for Decimal<K>
impl<const K: u32> ExactArith for Decimal<K>
Source§impl<const K: u32> FromStr for Decimal<K>
Parses whole or whole.fraction, exactly.
impl<const K: u32> FromStr for Decimal<K>
Parses whole or whole.fraction, exactly.
Both parts are plain ASCII digits; leading zeros are fine. The fraction has
1 to K digits, trailing zeros included, so nothing is ever rounded: "1.25" is a Decimal<2> or
Decimal<3> but not a Decimal<1>, and Decimal<0> takes integers only.
§Errors
ParseDecimalError::Emptyfor""or a missing whole part (".5").ParseDecimalError::InvalidDigitfor signs, whitespace, exponents, a second., or an empty fraction ("1.").ParseDecimalError::TooManyFractionDigitsfor more thanKfraction digits.ParseDecimalError::Overflowwhen the value exceedsu64::MAXraw units.
§Examples
use search_core::{Decimal, ParseDecimalError};
assert_eq!("3.25".parse::<Decimal<2>>(), Ok(Decimal::from_raw(325)));
assert_eq!("3.2".parse::<Decimal<2>>(), Ok(Decimal::from_raw(320)));
assert_eq!("7".parse::<Decimal<2>>(), Ok(Decimal::from_raw(700)));
assert_eq!("3.25".parse::<Decimal<1>>(), Err(ParseDecimalError::TooManyFractionDigits));
assert_eq!("-1".parse::<Decimal<2>>(), Err(ParseDecimalError::InvalidDigit));