Struct primitive_fixed_point_decimal::FixDec32
source · pub struct FixDec32<const P: u32> { /* private fields */ }
Expand description
A 32-bits primitive fixed-point decimal type, with about 9 significant digits.
See the module-level documentation for more information.
Implementations§
source§impl<const P: u32> FixDec32<P>
impl<const P: u32> FixDec32<P>
sourcepub const MIN_POSITIVE: Self = _
pub const MIN_POSITIVE: Self = _
The smallest positive value, 10-P .
sourcepub const fn abs(self) -> Self
pub const fn abs(self) -> Self
Computes the absolute value of self.
Overflow behavior
The absolute value of MIN
cannot be represented as this type,
and attempting to calculate it will cause an overflow. This means that
code in debug mode will trigger a panic on this case and optimized code
will return MIN
without a panic.
Examples
use primitive_fixed_point_decimal::FixDec32;
type Decimal = FixDec32::<4>;
assert_eq!(Decimal::ONE.abs(), Decimal::ONE);
assert_eq!(Decimal::MAX.abs(), Decimal::MAX);
assert_eq!((-Decimal::ONE).abs(), Decimal::ONE);
assert_eq!((-Decimal::MAX).abs(), Decimal::MAX);
assert_eq!(Decimal::ZERO.abs(), Decimal::ZERO);
sourcepub const fn checked_abs(self) -> Option<Self>
pub const fn checked_abs(self) -> Option<Self>
Checked absolute value. Computes self.abs()
, returning None
if self == MIN
.
Examples
use primitive_fixed_point_decimal::FixDec32;
type Decimal = FixDec32::<4>;
assert_eq!((-Decimal::ONE).checked_abs(), Some(Decimal::ONE));
assert_eq!(Decimal::MIN.checked_abs(), None);
sourcepub const fn checked_add(self, rhs: Self) -> Option<Self>
pub const fn checked_add(self, rhs: Self) -> Option<Self>
Checked addition. Computes self + rhs
, returning None
if overflow occurred.
The right operand must have the same precision with self. So you can not add
FixDec32::<4>
by FixDec32::<5>
.
If you really want to add a value with different precision, convert it by
FixDec32::rescale
first.
Examples
use std::str::FromStr;
use primitive_fixed_point_decimal::FixDec32;
type Decimal = FixDec32::<4>;
let left = Decimal::from_str("1.23").unwrap();
let right = Decimal::from_str("0.45").unwrap();
let res = Decimal::from_str("1.68").unwrap();
assert_eq!(left.checked_add(right), Some(res));
sourcepub const fn checked_sub(self, rhs: Self) -> Option<Self>
pub const fn checked_sub(self, rhs: Self) -> Option<Self>
Checked subtraction. Computes self - rhs
, returning None
if overflow occurred.
The right operand must have the same precision with self. So you can not subtract
FixDec32::<4>
by FixDec32::<5>
.
If you really want to subtract a value with different precision, convert it by
FixDec32::rescale
first.
Examples
use std::str::FromStr;
use primitive_fixed_point_decimal::FixDec32;
type Decimal = FixDec32::<4>;
let left = Decimal::from_str("1.68").unwrap();
let right = Decimal::from_str("1.23").unwrap();
let res = Decimal::from_str("0.45").unwrap();
assert_eq!(left.checked_sub(right), Some(res));
sourcepub const fn checked_mul_int(self, n: i32) -> Option<Self>
pub const fn checked_mul_int(self, n: i32) -> Option<Self>
Checked multiplication with integer. Computes self * n
, returning
None
if overflow occurred.
Examples
use std::str::FromStr;
use primitive_fixed_point_decimal::FixDec32;
type Decimal = FixDec32::<4>;
let dec = Decimal::from_str("0.123").unwrap();
let res = Decimal::from_str("1.23").unwrap();
assert_eq!(dec.checked_mul_int(10), Some(res));
sourcepub const fn checked_div_int(self, n: i32) -> Option<Self>
pub const fn checked_div_int(self, n: i32) -> Option<Self>
Checked division with integer. Equivalent to
FixDec32::checked_div_int_with_rounding
with rounding=Rounding::Round
.
Examples
use std::str::FromStr;
use primitive_fixed_point_decimal::FixDec32;
type Decimal = FixDec32::<4>;
let dec = Decimal::from_str("1.23").unwrap();
let res = Decimal::from_str("0.123").unwrap();
assert_eq!(dec.checked_div_int(10), Some(res));
assert_eq!(dec.checked_div_int(0), None);
sourcepub const fn checked_div_int_with_rounding(
self,
n: i32,
rounding: Rounding
) -> Option<Self>
pub const fn checked_div_int_with_rounding( self, n: i32, rounding: Rounding ) -> Option<Self>
Checked division with integer. Computes self / n
, returning
None
if n == 0
or precison loss with Rounding::Unexpected specified.
Examples
use std::str::FromStr;
use primitive_fixed_point_decimal::{FixDec32, Rounding};
type Decimal = FixDec32::<4>;
let dec = Decimal::from_str("0.2").unwrap();
let res1 = Decimal::from_str("0.0666").unwrap();
let res2 = Decimal::from_str("0.0667").unwrap();
assert_eq!(dec.checked_div_int_with_rounding(3, Rounding::Floor), Some(res1));
assert_eq!(dec.checked_div_int_with_rounding(3, Rounding::Ceil), Some(res2));
assert_eq!(dec.checked_div_int_with_rounding(3, Rounding::Unexpected), None);
sourcepub const fn checked_mul<const Q: u32, const R: u32>(
self,
rhs: FixDec32<Q>
) -> Option<FixDec32<R>>
pub const fn checked_mul<const Q: u32, const R: u32>( self, rhs: FixDec32<Q> ) -> Option<FixDec32<R>>
Checked multiplication. Equivalent to
FixDec32::checked_mul_with_rounding
with rounding=Rounding::Round
.
sourcepub const fn checked_mul_with_rounding<const Q: u32, const R: u32>(
self,
rhs: FixDec32<Q>,
rounding: Rounding
) -> Option<FixDec32<R>>
pub const fn checked_mul_with_rounding<const Q: u32, const R: u32>( self, rhs: FixDec32<Q>, rounding: Rounding ) -> Option<FixDec32<R>>
Checked multiplication. Computes self * rhs
, returning None
if overflow
occurred, or precison loss with Rounding::Unexpected specified.
The right operand and the result both could have different precisions
against Self. So you can multiple
FixDec32::<4>
by FixDec32::<3>
and get a FixDec32::<2>
.
Panics
Panics if P + Q - R > DIGITS
.
Examples
use std::str::FromStr;
use primitive_fixed_point_decimal::{FixDec32, Rounding};
type Balance = FixDec32::<4>;
type FeeRate = FixDec32::<3>; // different precision
let balance = Balance::from_str("2").unwrap();
let rate = FeeRate::from_str("0.015").unwrap();
let fee = Balance::from_str("0.03").unwrap();
assert_eq!(balance.checked_mul_with_rounding(rate, Rounding::Round), Some(fee));
sourcepub const fn checked_div<const Q: u32, const R: u32>(
self,
rhs: FixDec32<Q>
) -> Option<FixDec32<R>>
pub const fn checked_div<const Q: u32, const R: u32>( self, rhs: FixDec32<Q> ) -> Option<FixDec32<R>>
Checked division. Equivalent to
FixDec32::checked_div_with_rounding
with rounding=Rounding::Round
.
sourcepub const fn checked_div_with_rounding<const Q: u32, const R: u32>(
self,
rhs: FixDec32<Q>,
rounding: Rounding
) -> Option<FixDec32<R>>
pub const fn checked_div_with_rounding<const Q: u32, const R: u32>( self, rhs: FixDec32<Q>, rounding: Rounding ) -> Option<FixDec32<R>>
Checked division. Computes self / rhs
, returning None
if rhs == 0
or
the division results in overflow, or precison loss with Rounding::Unexpected specified.
The right operand and the result both could have different precisions
against Self. So you can divide
FixDec32::<4>
by FixDec32::<3>
and get a FixDec32::<2>
.
Panics
Panics if Q + R - P > DIGITS
.
Examples
use std::str::FromStr;
use primitive_fixed_point_decimal::{FixDec32, Rounding};
type Balance = FixDec32::<4>;
type FeeRate = FixDec32::<3>; // different precision
let balance = Balance::from_str("2").unwrap();
let fee = Balance::from_str("0.03").unwrap();
let rate = FeeRate::from_str("0.015").unwrap();
assert_eq!(fee.checked_div_with_rounding(balance, Rounding::Round), Some(rate));
assert_eq!(fee.checked_div_with_rounding(rate, Rounding::Round), Some(balance));
sourcepub const fn rescale<const Q: u32>(self) -> Option<FixDec32<Q>>
pub const fn rescale<const Q: u32>(self) -> Option<FixDec32<Q>>
Rescale to another precision representation.
Fail if overflow occurred when to bigger precision, or losing significant digits when to smaller precision.
Examples:
use std::str::FromStr;
use primitive_fixed_point_decimal::FixDec32;
type Dec2 = FixDec32::<2>;
type Dec4 = FixDec32::<4>;
let d2 = Dec2::from_str("1.23").unwrap();
let d4 = Dec4::from_str("1.23").unwrap();
assert_eq!(d4.rescale::<2>().unwrap(), d2);
assert_eq!(d2.rescale::<4>().unwrap(), d4);
sourcepub const fn shrink_to(self, precision: i32) -> Self
pub const fn shrink_to(self, precision: i32) -> Self
Shrink to a lower precision. Equivalent to
FixDec32::shrink_to_with_rounding
with rounding=Rounding::Round
.
sourcepub const fn shrink_to_with_rounding(
self,
precision: i32,
rounding: Rounding
) -> Option<Self>
pub const fn shrink_to_with_rounding( self, precision: i32, rounding: Rounding ) -> Option<Self>
Shrink to a lower precision. Fail if lossing significant precision
with rounding=Rounding::Unexpected
.
Negative precision argument means integer part.
Examples:
use std::str::FromStr;
use primitive_fixed_point_decimal::{FixDec32, Rounding};
type Decimal = FixDec32::<4>;
let d = Decimal::from_str("1.2378").unwrap();
assert_eq!(d.shrink_to_with_rounding(2, Rounding::Floor).unwrap(), Decimal::from_str("1.23").unwrap());
assert_eq!(d.shrink_to_with_rounding(2, Rounding::Ceil).unwrap(), Decimal::from_str("1.24").unwrap());
assert_eq!(d.shrink_to_with_rounding(2, Rounding::Round).unwrap(), Decimal::from_str("1.24").unwrap());
assert_eq!(d.shrink_to_with_rounding(2, Rounding::Unexpected), None);
// negative precision argument
type Decimal1 = FixDec32::<1>;
let d = Decimal1::from_str("1234.5").unwrap();
assert_eq!(d.shrink_to_with_rounding(-2, Rounding::Round).unwrap(), Decimal1::from_str("1200").unwrap());
sourcepub const fn from_inner(inner: i32) -> Self
pub const fn from_inner(inner: i32) -> Self
Construct from inner directly. This API is low-level. Use it carefully.
Making a FixDec32<P> from inner
gets value: inner-P.
If you want to convert an integer to Fixdec keeping its value, use
FixDec32::try_from
.
Examples:
use std::str::FromStr;
use primitive_fixed_point_decimal::FixDec32;
type Decimal = FixDec32::<4>;
assert_eq!(Decimal::from_inner(12345), Decimal::from_str("1.2345").unwrap());
sourcepub fn from_str_with_rounding(
s: &str,
rounding: Rounding
) -> Result<Self, ParseError>
pub fn from_str_with_rounding( s: &str, rounding: Rounding ) -> Result<Self, ParseError>
Read decimal from string, with specified rounding kind.
Examples:
use std::str::FromStr;
use primitive_fixed_point_decimal::{Rounding, ParseError};
use primitive_fixed_point_decimal::FixDec32;
type Decimal = FixDec32::<4>;
fn check(origin: &str, rounding: Rounding, expect: &str) {
let fd = Decimal::from_str_with_rounding(origin, rounding).unwrap();
assert_eq!(fd, Decimal::from_str(expect).unwrap());
}
check("1.23456789", Rounding::Floor, "1.2345");
check("1.23456789", Rounding::Ceil, "1.2346");
check("1.23456789", Rounding::Round, "1.2346");
check("1.23455000", Rounding::Round, "1.2346");
check("1.23", Rounding::Round, "1.23");
assert_eq!(Decimal::from_str_with_rounding("1.23789", Rounding::Unexpected),
Err(ParseError::Precision));
Trait Implementations§
source§impl<const P: u32> AddAssign for FixDec32<P>
impl<const P: u32> AddAssign for FixDec32<P>
source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+=
operation. Read moresource§impl<const P: u32> Display for FixDec32<P>
impl<const P: u32> Display for FixDec32<P>
Format the decimal.
The tailing zeros of fraction are truncated by default, while the
precision can be specified by {:.N}
.
Examples:
use std::str::FromStr;
use primitive_fixed_point_decimal::FixDec32;
type Decimal = FixDec32::<4>;
let fd = Decimal::from_str("1.5670").unwrap();
assert_eq!(&format!("{}", fd), "1.567"); // omit tailing zeros
assert_eq!(&format!("{:.2}", fd), "1.57"); // rounding
source§impl<const P: u32> FromStr for FixDec32<P>
impl<const P: u32> FromStr for FixDec32<P>
source§fn from_str(s: &str) -> Result<Self, ParseError>
fn from_str(s: &str) -> Result<Self, ParseError>
Read decimal from string.
Equivalent to FixDec32::from_str_with_rounding
with rounding=Rounding::Round
.
§type Err = ParseError
type Err = ParseError
source§impl<const P: u32> Ord for FixDec32<P>
impl<const P: u32> Ord for FixDec32<P>
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl<const P: u32> PartialEq for FixDec32<P>
impl<const P: u32> PartialEq for FixDec32<P>
source§impl<const P: u32> PartialOrd for FixDec32<P>
impl<const P: u32> PartialOrd for FixDec32<P>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<const P: u32> SubAssign for FixDec32<P>
impl<const P: u32> SubAssign for FixDec32<P>
source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-=
operation. Read moresource§impl<const P: u32> TryFrom<f32> for FixDec32<P>
impl<const P: u32> TryFrom<f32> for FixDec32<P>
source§fn try_from(f: f32) -> Result<Self, Self::Error>
fn try_from(f: f32) -> Result<Self, Self::Error>
Try to convert f32 into FixDec. Fail if overflow occurred.
Examples:
use std::str::FromStr;
use primitive_fixed_point_decimal::FixDec32;
type Decimal = FixDec32::<2>;
assert_eq!(Decimal::try_from(123.456_f32).unwrap(), Decimal::from_str("123.46").unwrap());
source§impl<const P: u32> TryFrom<f64> for FixDec32<P>
impl<const P: u32> TryFrom<f64> for FixDec32<P>
source§fn try_from(f: f64) -> Result<Self, Self::Error>
fn try_from(f: f64) -> Result<Self, Self::Error>
Try to convert f64 into FixDec. Fail if overflow occurred.
Examples:
use std::str::FromStr;
use primitive_fixed_point_decimal::FixDec32;
type Decimal = FixDec32::<2>;
assert_eq!(Decimal::try_from(123.456_f64).unwrap(), Decimal::from_str("123.46").unwrap());
source§impl<const P: u32> TryFrom<i128> for FixDec32<P>
impl<const P: u32> TryFrom<i128> for FixDec32<P>
source§fn try_from(i: i128) -> Result<Self, Self::Error>
fn try_from(i: i128) -> Result<Self, Self::Error>
Try to convert i128 into FixDec. Fail if overflow occurred.
Examples:
use std::str::FromStr;
use primitive_fixed_point_decimal::FixDec32;
type Decimal = FixDec32::<2>;
assert_eq!(Decimal::try_from(100_i128).unwrap(), Decimal::from_str("100").unwrap());
source§impl<const P: u32> TryFrom<i16> for FixDec32<P>
impl<const P: u32> TryFrom<i16> for FixDec32<P>
source§fn try_from(i: i16) -> Result<Self, Self::Error>
fn try_from(i: i16) -> Result<Self, Self::Error>
Try to convert i16 into FixDec. Fail if overflow occurred.
Examples:
use std::str::FromStr;
use primitive_fixed_point_decimal::FixDec32;
type Decimal = FixDec32::<2>;
assert_eq!(Decimal::try_from(100_i16).unwrap(), Decimal::from_str("100").unwrap());
source§impl<const P: u32> TryFrom<i32> for FixDec32<P>
impl<const P: u32> TryFrom<i32> for FixDec32<P>
source§fn try_from(i: i32) -> Result<Self, Self::Error>
fn try_from(i: i32) -> Result<Self, Self::Error>
Try to convert i32 into FixDec. Fail if overflow occurred.
Examples:
use std::str::FromStr;
use primitive_fixed_point_decimal::FixDec32;
type Decimal = FixDec32::<2>;
assert_eq!(Decimal::try_from(100_i32).unwrap(), Decimal::from_str("100").unwrap());
source§impl<const P: u32> TryFrom<i64> for FixDec32<P>
impl<const P: u32> TryFrom<i64> for FixDec32<P>
source§fn try_from(i: i64) -> Result<Self, Self::Error>
fn try_from(i: i64) -> Result<Self, Self::Error>
Try to convert i64 into FixDec. Fail if overflow occurred.
Examples:
use std::str::FromStr;
use primitive_fixed_point_decimal::FixDec32;
type Decimal = FixDec32::<2>;
assert_eq!(Decimal::try_from(100_i64).unwrap(), Decimal::from_str("100").unwrap());
source§impl<const P: u32> TryFrom<i8> for FixDec32<P>
impl<const P: u32> TryFrom<i8> for FixDec32<P>
source§fn try_from(i: i8) -> Result<Self, Self::Error>
fn try_from(i: i8) -> Result<Self, Self::Error>
Try to convert i8 into FixDec. Fail if overflow occurred.
Examples:
use std::str::FromStr;
use primitive_fixed_point_decimal::FixDec32;
type Decimal = FixDec32::<2>;
assert_eq!(Decimal::try_from(100_i8).unwrap(), Decimal::from_str("100").unwrap());
source§impl<const P: u32> TryFrom<u128> for FixDec32<P>
impl<const P: u32> TryFrom<u128> for FixDec32<P>
source§fn try_from(i: u128) -> Result<Self, Self::Error>
fn try_from(i: u128) -> Result<Self, Self::Error>
Try to convert u128 into FixDec. Fail if overflow occurred.
Examples:
use std::str::FromStr;
use primitive_fixed_point_decimal::FixDec32;
type Decimal = FixDec32::<2>;
assert_eq!(Decimal::try_from(100_u128).unwrap(), Decimal::from_str("100").unwrap());
source§impl<const P: u32> TryFrom<u16> for FixDec32<P>
impl<const P: u32> TryFrom<u16> for FixDec32<P>
source§fn try_from(i: u16) -> Result<Self, Self::Error>
fn try_from(i: u16) -> Result<Self, Self::Error>
Try to convert u16 into FixDec. Fail if overflow occurred.
Examples:
use std::str::FromStr;
use primitive_fixed_point_decimal::FixDec32;
type Decimal = FixDec32::<2>;
assert_eq!(Decimal::try_from(100_u16).unwrap(), Decimal::from_str("100").unwrap());
source§impl<const P: u32> TryFrom<u32> for FixDec32<P>
impl<const P: u32> TryFrom<u32> for FixDec32<P>
source§fn try_from(i: u32) -> Result<Self, Self::Error>
fn try_from(i: u32) -> Result<Self, Self::Error>
Try to convert u32 into FixDec. Fail if overflow occurred.
Examples:
use std::str::FromStr;
use primitive_fixed_point_decimal::FixDec32;
type Decimal = FixDec32::<2>;
assert_eq!(Decimal::try_from(100_u32).unwrap(), Decimal::from_str("100").unwrap());
source§impl<const P: u32> TryFrom<u64> for FixDec32<P>
impl<const P: u32> TryFrom<u64> for FixDec32<P>
source§fn try_from(i: u64) -> Result<Self, Self::Error>
fn try_from(i: u64) -> Result<Self, Self::Error>
Try to convert u64 into FixDec. Fail if overflow occurred.
Examples:
use std::str::FromStr;
use primitive_fixed_point_decimal::FixDec32;
type Decimal = FixDec32::<2>;
assert_eq!(Decimal::try_from(100_u64).unwrap(), Decimal::from_str("100").unwrap());
source§impl<const P: u32> TryFrom<u8> for FixDec32<P>
impl<const P: u32> TryFrom<u8> for FixDec32<P>
source§fn try_from(i: u8) -> Result<Self, Self::Error>
fn try_from(i: u8) -> Result<Self, Self::Error>
Try to convert u8 into FixDec. Fail if overflow occurred.
Examples:
use std::str::FromStr;
use primitive_fixed_point_decimal::FixDec32;
type Decimal = FixDec32::<2>;
assert_eq!(Decimal::try_from(100_u8).unwrap(), Decimal::from_str("100").unwrap());