pub struct Quantity<const SCALE: u32>(/* private fields */);Expand description
A decimal quantity stored as an i64 count of 10^-SCALE units.
SCALE is at most 9, checked at compile time. Adding and subtracting
return a Result; there are no panicking operators.
use renox::money::Rounding;
use renox::quantity::Quantity;
let a = Quantity::<3>::parse_exact("1.250").unwrap();
let b = Quantity::<3>::parse_exact("0.750").unwrap();
assert_eq!(a.checked_add(b).unwrap().to_string(), "2.000");
assert_eq!(a.mul_decimal("1.15", Rounding::HalfUp).unwrap().to_string(), "1.438");
assert!(b < a);Implementations§
Source§impl<const SCALE: u32> Quantity<SCALE>
impl<const SCALE: u32> Quantity<SCALE>
Sourcepub const fn from_raw(raw: i64) -> Self
pub const fn from_raw(raw: i64) -> Self
The quantity of raw units of 10^-SCALE (2500 is 2.500 for
Quantity<3>).
use renox::quantity::Quantity;
assert_eq!(Quantity::<3>::from_raw(2500).to_string(), "2.500");Sourcepub const fn raw(self) -> i64
pub const fn raw(self) -> i64
The value in units of 10^-SCALE.
use renox::quantity::Quantity;
assert_eq!(Quantity::<2>::from_raw(-150).raw(), -150);Sourcepub fn parse(text: &str, rounding: Rounding) -> Result<Self, QuantityError>
pub fn parse(text: &str, rounding: Rounding) -> Result<Self, QuantityError>
Reads a decimal such as "2.5" or "-0.125" with . as the
decimal point. Digits beyond the scale are rounded with rounding
(Down is toward zero, Up away from zero). Empty text, signs
other than a leading - or +, exponents and separators are
Invalid; a value that does not fit is Overflow.
use renox::money::Rounding;
use renox::quantity::Quantity;
let q = |t, r| Quantity::<3>::parse(t, r).unwrap().to_string();
assert_eq!(q("2.5", Rounding::HalfEven), "2.500");
assert_eq!(q("0.0005", Rounding::HalfUp), "0.001");
assert_eq!(q("0.0005", Rounding::HalfEven), "0.000");
assert_eq!(q("-0.0005", Rounding::Up), "-0.001");
assert_eq!(q("0.0009", Rounding::Down), "0.000");
assert!(Quantity::<3>::parse("1e3", Rounding::Down).is_err());Sourcepub fn parse_exact(text: &str) -> Result<Self, QuantityError>
pub fn parse_exact(text: &str) -> Result<Self, QuantityError>
Reads a decimal and fails with Inexact if digits beyond the scale
are not all zero; nothing is rounded.
use renox::quantity::{Quantity, QuantityError};
assert_eq!(Quantity::<3>::parse_exact("12.50").unwrap().raw(), 12_500);
assert_eq!(Quantity::<3>::parse_exact("1.2000").unwrap().raw(), 1_200);
assert_eq!(Quantity::<3>::parse_exact("1.0001").unwrap_err(), QuantityError::Inexact);Sourcepub fn parse_half_even(text: &str) -> Result<Self, QuantityError>
pub fn parse_half_even(text: &str) -> Result<Self, QuantityError>
Like Quantity::parse with HalfEven (banker’s rounding). It is
a convenience, not a default: parse always asks for a rounding.
use renox::quantity::Quantity;
assert_eq!(Quantity::<0>::parse_half_even("2.5").unwrap().raw(), 2);
assert_eq!(Quantity::<0>::parse_half_even("3.5").unwrap().raw(), 4);Sourcepub fn checked_add(self, other: Self) -> Result<Self, QuantityError>
pub fn checked_add(self, other: Self) -> Result<Self, QuantityError>
The sum, or Overflow past the i64 limits.
use renox::quantity::Quantity;
let one = Quantity::<3>::from_raw(1000);
assert_eq!(one.checked_add(one).unwrap().raw(), 2000);
assert!(Quantity::<3>::from_raw(i64::MAX).checked_add(one).is_err());Sourcepub fn checked_sub(self, other: Self) -> Result<Self, QuantityError>
pub fn checked_sub(self, other: Self) -> Result<Self, QuantityError>
The difference, or Overflow past the i64 limits.
use renox::quantity::Quantity;
let one = Quantity::<3>::from_raw(1000);
assert_eq!(one.checked_sub(Quantity::from_raw(1500)).unwrap().raw(), -500);Sourcepub fn checked_neg(self) -> Result<Self, QuantityError>
pub fn checked_neg(self) -> Result<Self, QuantityError>
The negated quantity, or Overflow for the smallest i64.
use renox::quantity::Quantity;
assert_eq!(Quantity::<3>::from_raw(5).checked_neg().unwrap().raw(), -5);
assert!(Quantity::<3>::from_raw(i64::MIN).checked_neg().is_err());Sourcepub fn mul_ratio(
self,
num: i64,
den: i64,
rounding: Rounding,
) -> Result<Self, QuantityError>
pub fn mul_ratio( self, num: i64, den: i64, rounding: Rounding, ) -> Result<Self, QuantityError>
The quantity times num / den, rounded to the scale with
rounding. A zero den is DivisionByZero.
use renox::money::Rounding;
use renox::quantity::Quantity;
let q = Quantity::<3>::from_raw(1000);
assert_eq!(q.mul_ratio(1, 3, Rounding::Down).unwrap().raw(), 333);
assert_eq!(q.mul_ratio(1, 3, Rounding::Up).unwrap().raw(), 334);Sourcepub fn mul_decimal(
self,
factor: &str,
rounding: Rounding,
) -> Result<Self, QuantityError>
pub fn mul_decimal( self, factor: &str, rounding: Rounding, ) -> Result<Self, QuantityError>
The quantity times an exact decimal factor such as "1.15",
rounded with rounding. The factor has at most 18 decimals and no
exponent; anything else is Invalid.
use renox::money::Rounding;
use renox::quantity::Quantity;
let q = Quantity::<3>::from_raw(2000);
assert_eq!(q.mul_decimal("1.15", Rounding::HalfUp).unwrap().raw(), 2300);
assert_eq!(q.mul_decimal("-0.0005", Rounding::HalfUp).unwrap().raw(), -1);
assert!(q.mul_decimal("1e3", Rounding::HalfUp).is_err());Sourcepub fn rescale<const TO: u32>(
self,
rounding: Rounding,
) -> Result<Quantity<TO>, QuantityError>
pub fn rescale<const TO: u32>( self, rounding: Rounding, ) -> Result<Quantity<TO>, QuantityError>
The same value at another scale. Going to a finer scale is exact
(or Overflow); going coarser rounds with rounding.
use renox::money::Rounding;
use renox::quantity::Quantity;
let q = Quantity::<3>::from_raw(2505);
assert_eq!(q.rescale::<2>(Rounding::HalfUp).unwrap().raw(), 251);
assert_eq!(q.rescale::<2>(Rounding::Down).unwrap().raw(), 250);
assert_eq!(q.rescale::<4>(Rounding::Down).unwrap().raw(), 25_050);Trait Implementations§
Source§impl<const S: u32> ColumnType for Quantity<S>
A Quantity is one BIGINT column; the scale is in the Rust type only.
impl<const S: u32> ColumnType for Quantity<S>
A Quantity is one BIGINT column; the scale is in the Rust type only.
Source§const KIND: ColumnKind = ColumnKind::BigInt
const KIND: ColumnKind = ColumnKind::BigInt
impl<const SCALE: u32> Copy for Quantity<SCALE>
Source§impl<'de, const SCALE: u32> Deserialize<'de> for Quantity<SCALE>
impl<'de, const SCALE: u32> Deserialize<'de> for Quantity<SCALE>
Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
Accepts a decimal string, or a JSON number when it is exactly representable at the scale (its shortest decimal form is read).
impl<const SCALE: u32> Eq for Quantity<SCALE>
impl<const S: u32> Number for Quantity<S>
The sum of a Quantity column stays a Quantity of the same scale.
Source§impl<const SCALE: u32> Ord for Quantity<SCALE>
impl<const SCALE: u32> Ord for Quantity<SCALE>
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<const SCALE: u32> PartialOrd for Quantity<SCALE>
impl<const SCALE: u32> PartialOrd for Quantity<SCALE>
impl<const SCALE: u32> StructuralPartialEq for Quantity<SCALE>
Source§impl<const S: u32> ToDbValue for Quantity<S>
The scaled integer, stored as BIGINT: exact on both databases. The scale
lives in the Rust type, not in the schema.
impl<const S: u32> ToDbValue for Quantity<S>
The scaled integer, stored as BIGINT: exact on both databases. The scale
lives in the Rust type, not in the schema.
Source§fn to_db_value(&self) -> DbValue
fn to_db_value(&self) -> DbValue
self.Auto Trait Implementations§
impl<const SCALE: u32> Freeze for Quantity<SCALE>
impl<const SCALE: u32> RefUnwindSafe for Quantity<SCALE>
impl<const SCALE: u32> Send for Quantity<SCALE>
impl<const SCALE: u32> Sync for Quantity<SCALE>
impl<const SCALE: u32> Unpin for Quantity<SCALE>
impl<const SCALE: u32> UnsafeUnpin for Quantity<SCALE>
impl<const SCALE: u32> UnwindSafe for Quantity<SCALE>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
impl<T> Comparable<T> for Twhere
T: ToDbValue,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
impl<T> FromDb for T
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more