pub struct OobScaleFpdec<I>(/* private fields */);Expand description
Out-of-band-scale fixed-point decimal.
I is the inner integer type. It could be signed i8, i16, i32,
i64, i128, or unsigned u8, u16, u32, u64, u128, with
about 2, 4, 9, 18/19 and 38 significant digits respectively.
For example, OobScaleFpdec<i64> means using i64 as the underlying
integer. It’s your job to save the out-of-band scale somewhere else.
The scale can be positive for fraction precision or be negative for omitting the low-order digits of integer values.
Compared to ConstScaleFpdec, this OobScaleFpdec has more verbose APIs:
- extra
diff_scaleargument for most operations such as*and/, but no need for+and-, - use
try_from_str()to convert from string with scale set, - use
(*, i32)tuple for converting from integers or floats, - use
to_f32()orto_f64()to convert to floats, - use
OobFmtforDisplayandFromStr, - no associate const
SCALE, - and others.
See the module-level documentation for more information.
Implementations§
Source§impl<I> OobScaleFpdec<I>where
I: FpdecInner,
impl<I> OobScaleFpdec<I>where
I: FpdecInner,
Sourcepub const MAX_POWERS: Self
pub const MAX_POWERS: Self
The largest powers of 10.
Sourcepub fn checked_add(self, rhs: Self) -> Option<Self>
pub fn checked_add(self, rhs: Self) -> Option<Self>
Checked addition. Computes self + rhs, returning None if overflow occurred.
If you make sure no overflow error, you can use + and += instead
for convenience.
The right operand must have the same scale with self.
Sourcepub fn checked_sub(self, rhs: Self) -> Option<Self>
pub fn checked_sub(self, rhs: Self) -> Option<Self>
Checked subtraction. Computes self - rhs, returning None if overflow occurred.
If you make sure no overflow error, you can use - and -= instead
for convenience.
The right operand must have the same scale with self.
Sourcepub fn checked_mul_int(self, n: impl Into<I>) -> Option<Self>
pub fn checked_mul_int(self, n: impl Into<I>) -> Option<Self>
Checked multiplication with integer. Computes self * n, returning
None if overflow occurred.
If you make sure no overflow error, you can use * and *= instead
for convenience.
Sourcepub fn checked_mul_ratio<R>(self, a: R, b: R) -> Option<Self>where
R: IntoRatioInt<I>,
pub fn checked_mul_ratio<R>(self, a: R, b: R) -> Option<Self>where
R: IntoRatioInt<I>,
Computes self * a/b, returning None if overflow occurred.
Equivalent to Self::checked_mul_ratio_ext with Rounding::Round.
Sourcepub fn checked_mul_ratio_ext<R>(
self,
a: R,
b: R,
rounding: Rounding,
) -> Option<Self>where
R: IntoRatioInt<I>,
pub fn checked_mul_ratio_ext<R>(
self,
a: R,
b: R,
rounding: Rounding,
) -> Option<Self>where
R: IntoRatioInt<I>,
Computes self * a/b, returning None if overflow occurred.
The arguments a and b could be primitive integers,
ConstScaleFpdec or OobScaleFpdec with same scale.
Compared to calling Self::checked_mul_int and Self::checked_div_int
separately, this can avoid some potential overflow.
Examples:
// Here we use `ConstScaleFpdec` as example. It's same for `OobScaleFpdec`.
use primitive_fixed_point_decimal::{ConstScaleFpdec, fpdec};
type Balance = ConstScaleFpdec<i64, 2>;
type Quantity = ConstScaleFpdec<i32, 4>; // type for `a` and `b`
let margin: Balance = fpdec!(30000);
let deal: Quantity = fpdec!(0.2);
let total: Quantity = fpdec!(0.3);
assert_eq!(margin.checked_mul_ratio(deal, total).unwrap(), fpdec!(20000));
// integer
assert_eq!(margin.checked_mul_ratio(200, 300).unwrap(), fpdec!(20000));Sourcepub fn checked_div_int(self, n: impl Into<I>) -> Option<Self>
pub fn checked_div_int(self, n: impl Into<I>) -> Option<Self>
Checked division by integer, with Rounding::Round.
Computes self / n, returning None if n == 0 or overflow occurres.
If you make sure no overflow or 0-division error, you can use
/ and /= instead for convenience.
Sourcepub fn checked_div_int_ext(
self,
n: impl Into<I>,
rounding: Rounding,
) -> Option<Self>
pub fn checked_div_int_ext( self, n: impl Into<I>, rounding: Rounding, ) -> Option<Self>
Checked division by integer with rounding.
Computes self / n, returning None if n == 0 or overflow occurres.
Sourcepub const fn from_mantissa(i: I) -> Self
pub const fn from_mantissa(i: I) -> Self
Create a decimal from the underlying integer representation.
You must take care of the scale yourself.
This method is const, so it can be used to construct const values.
Examples:
// Here we use `ConstScaleFpdec` as example. It's same for `OobScaleFpdec`.
use primitive_fixed_point_decimal::{ConstScaleFpdec, fpdec};
type Dec = ConstScaleFpdec<i32, 4>; // scale is 4
assert_eq!(Dec::from_mantissa(123400), fpdec!(12.34));
// const values
const ONE: Dec = Dec::from_mantissa(1 * 10000);
const TEN: Dec = Dec::from_mantissa(10 * 10000);
const PI: Dec = Dec::from_mantissa(31400);
assert_eq!(ONE, fpdec!(1));
assert_eq!(TEN, fpdec!(10));
assert_eq!(PI, fpdec!(3.14));
// you can use `Dec::SCALE` for better consistency
const ONE_1: Dec = Dec::from_mantissa(10_i32.pow(Dec::SCALE as u32));
const TEN_1: Dec = Dec::from_mantissa(10_i32.pow(Dec::SCALE as u32 + 1));
const PI_1: Dec = Dec::from_mantissa(314 * 10_i32.pow(Dec::SCALE as u32 - 2));
assert_eq!(ONE_1, fpdec!(1));
assert_eq!(TEN_1, fpdec!(10));
assert_eq!(PI_1, fpdec!(3.14));Sourcepub const fn mantissa(self) -> I
pub const fn mantissa(self) -> I
Return the underlying integer representation.
You must take care of the scale yourself.
Examples:
// Here we use `ConstScaleFpdec` as example. It's same for `OobScaleFpdec`.
use primitive_fixed_point_decimal::{ConstScaleFpdec, fpdec};
type Dec = ConstScaleFpdec<i32, 4>; // scale is 4
let d: Dec = fpdec!(12.34);
assert_eq!(d.mantissa(), 123400);Sourcepub fn checked_mul<J>(
self,
rhs: OobScaleFpdec<J>,
diff_scale: i32,
) -> Option<OobScaleFpdec<I>>where
J: FpdecInner,
pub fn checked_mul<J>(
self,
rhs: OobScaleFpdec<J>,
diff_scale: i32,
) -> Option<OobScaleFpdec<I>>where
J: FpdecInner,
Checked multiplication.
Equivalent to Self::checked_mul_ext with Rounding::Round.
Sourcepub fn checked_mul_ext<J>(
self,
rhs: OobScaleFpdec<J>,
diff_scale: i32,
rounding: Rounding,
) -> Option<OobScaleFpdec<I>>where
J: FpdecInner,
pub fn checked_mul_ext<J>(
self,
rhs: OobScaleFpdec<J>,
diff_scale: i32,
rounding: Rounding,
) -> Option<OobScaleFpdec<I>>where
J: FpdecInner,
Checked multiplication. Computes self * rhs, returning None if
overflow occurred or argument diff_scale is out of range
[-Self::DIGITS, Self::DIGITS].
The type of rhs can have different inner integer J,
while the type of result must have the same I.
Argument: diff_scale = scale(self) + scale(rhs) - scale(result).
If the diff_scale < 0, then rounding operations are required and precision may be lost. You can specify the rounding type.
§Examples
use primitive_fixed_point_decimal::{OobScaleFpdec, Rounding, fpdec};
type Balance = OobScaleFpdec<i64>;
type FeeRate = OobScaleFpdec<i16>; // different types
let balance: Balance = fpdec!(12.30, 2); // scale=2
let rate: FeeRate = fpdec!(0.01, 4); // scale=4
let fee: Balance = balance.checked_mul(rate, 4).unwrap();
assert_eq!(fee, fpdec!(0.12, 2));
let fee: Balance = balance.checked_mul_ext(rate, 4, Rounding::Ceiling).unwrap();
assert_eq!(fee, fpdec!(0.13, 2));Sourcepub fn checked_mul_const_scale<J, const S: i32>(
self,
rhs: ConstScaleFpdec<J, S>,
) -> Option<OobScaleFpdec<I>>where
J: FpdecInner,
pub fn checked_mul_const_scale<J, const S: i32>(
self,
rhs: ConstScaleFpdec<J, S>,
) -> Option<OobScaleFpdec<I>>where
J: FpdecInner,
Checked multiplication with ConstScaleFpdec.
Equivalent to Self::checked_mul_const_scale_ext with Rounding::Round.
If you make sure no overflow error, you can use * and *= instead for convenience.
Sourcepub fn checked_mul_const_scale_ext<J, const S: i32>(
self,
rhs: ConstScaleFpdec<J, S>,
rounding: Rounding,
) -> Option<OobScaleFpdec<I>>where
J: FpdecInner,
pub fn checked_mul_const_scale_ext<J, const S: i32>(
self,
rhs: ConstScaleFpdec<J, S>,
rounding: Rounding,
) -> Option<OobScaleFpdec<I>>where
J: FpdecInner,
Checked multiplication with ConstScaleFpdec. Computes self * rhs,
returning None if overflow occurred.
Compared to Self::checked_mul_ext, the result of this method inherits
the scale of self. So the diff_scale (which is equal to S) is no
more need.
§Examples
use primitive_fixed_point_decimal::{ConstScaleFpdec, OobScaleFpdec, Rounding, fpdec};
type Balance = OobScaleFpdec<i64>;
type FeeRate = ConstScaleFpdec<i16, 4>;
let balance: Balance = fpdec!(12.30, 2); // scale=2
let rate: FeeRate = fpdec!(0.01);
let fee: Balance = balance.checked_mul_const_scale(rate).unwrap();
assert_eq!(fee, fpdec!(0.12, 2));
let fee: Balance = balance.checked_mul_const_scale_ext(rate, Rounding::Ceiling).unwrap();
assert_eq!(fee, fpdec!(0.13, 2));Sourcepub fn checked_div<J>(
self,
rhs: OobScaleFpdec<J>,
diff_scale: i32,
) -> Option<OobScaleFpdec<I>>where
J: FpdecInner,
pub fn checked_div<J>(
self,
rhs: OobScaleFpdec<J>,
diff_scale: i32,
) -> Option<OobScaleFpdec<I>>where
J: FpdecInner,
Checked division.
Equivalent to Self::checked_div_ext with Rounding::Round.
Sourcepub fn checked_div_ext<J>(
self,
rhs: OobScaleFpdec<J>,
diff_scale: i32,
rounding: Rounding,
) -> Option<OobScaleFpdec<I>>where
J: FpdecInner,
pub fn checked_div_ext<J>(
self,
rhs: OobScaleFpdec<J>,
diff_scale: i32,
rounding: Rounding,
) -> Option<OobScaleFpdec<I>>where
J: FpdecInner,
Checked division. Computes self / rhs, returning None if
division by 0, or overflow occurred, or argument diff_scale
is out of range [-Self::DIGITS, Self::DIGITS].
The type of rhs can have different inner integer J,
while the type of result must have the same I.
Argument: diff_scale = scale(self) - scale(rhs) - scale(result).
You can specify the rounding type.
§Examples
use primitive_fixed_point_decimal::{OobScaleFpdec, Rounding, fpdec};
type Balance = OobScaleFpdec<i64>;
type FeeRate = OobScaleFpdec<i16>; // different types
let fee: Balance = fpdec!(0.13, 2); // scale=2
let rate: FeeRate = fpdec!(0.03, 4); // scale=4
let balance: Balance = fee.checked_div_ext(rate, -4, Rounding::Ceiling).unwrap();
assert_eq!(balance, fpdec!(4.34, 2));Sourcepub fn checked_div_const_scale<J, const S: i32>(
self,
rhs: ConstScaleFpdec<J, S>,
) -> Option<OobScaleFpdec<I>>where
J: FpdecInner,
pub fn checked_div_const_scale<J, const S: i32>(
self,
rhs: ConstScaleFpdec<J, S>,
) -> Option<OobScaleFpdec<I>>where
J: FpdecInner,
Checked division with ConstScaleFpdec.
Equivalent to Self::checked_div_const_scale_ext with Rounding::Round.
If you make sure no overflow or 0-division error, you can use / and /=
instead for convenience.
Sourcepub fn checked_div_const_scale_ext<J, const S: i32>(
self,
rhs: ConstScaleFpdec<J, S>,
rounding: Rounding,
) -> Option<OobScaleFpdec<I>>where
J: FpdecInner,
pub fn checked_div_const_scale_ext<J, const S: i32>(
self,
rhs: ConstScaleFpdec<J, S>,
rounding: Rounding,
) -> Option<OobScaleFpdec<I>>where
J: FpdecInner,
Checked division with ConstScaleFpdec. Computes self / rhs,
returning None if division by 0 or overflow occurred.
Compared to Self::checked_div_ext, the result of this method inherits
the scale of self. So the diff_scale (which is equal to -S) is no
more need.
§Examples
use primitive_fixed_point_decimal::{ConstScaleFpdec, OobScaleFpdec, Rounding, fpdec};
type Balance = OobScaleFpdec<i64>;
type FeeRate = ConstScaleFpdec<i16, 4>;
let fee: Balance = fpdec!(0.12, 2); // scale=2
let rate: FeeRate = fpdec!(0.01);
let balance: Balance = fee.checked_div_const_scale(rate).unwrap();
assert_eq!(balance, fpdec!(12.0, 2));Sourcepub fn round_diff(self, diff_scale: i32) -> Self
pub fn round_diff(self, diff_scale: i32) -> Self
Round the decimal.
Equivalent to Self::round_diff_ext with Rounding::Round.
Sourcepub fn round_diff_ext(self, diff_scale: i32, rounding: Rounding) -> Self
pub fn round_diff_ext(self, diff_scale: i32, rounding: Rounding) -> Self
Round the decimal with rounding type.
The argument diff_scale is original_scale - round_scale.
Return the original decimal if diff_scale <= 0.
Examples:
use primitive_fixed_point_decimal::{OobScaleFpdec, Rounding, fpdec};
type Price = OobScaleFpdec<i64>;
let price: Price = fpdec!(12.12345678, 8); // scale=8
assert_eq!(price.round_diff(8 - 6), // reduce 2 scale
fpdec!(12.123457, 8)); // `Rounding::Round` as default
assert_eq!(price.round_diff_ext(8 - 6, Rounding::Floor),
fpdec!(12.123456, 8));Sourcepub fn try_from_str(s: &str, scale: i32) -> Result<Self, ParseError>where
I: Num<FromStrRadixErr = ParseIntError>,
pub fn try_from_str(s: &str, scale: i32) -> Result<Self, ParseError>where
I: Num<FromStrRadixErr = ParseIntError>,
Read decimal from string.
This method has 2 limitations:
- Support decimal format only but not scientific notation;
- Return
ParseError::Precisionif the string has more precision.
If you want to skip these limitations, you can parse the string to float number first and then convert the number to this decimal.
Examples:
use primitive_fixed_point_decimal::{OobScaleFpdec, ParseError, fpdec};
type Decimal = OobScaleFpdec<i16>;
assert_eq!(Decimal::try_from_str("1.23", 4).unwrap(), fpdec!(1.23, 4));
assert_eq!(Decimal::try_from_str("9999", 4), Err(ParseError::Overflow));
assert_eq!(Decimal::try_from_str("1.23456", 4), Err(ParseError::Precision));Sourcepub fn to_f32(self, scale: i32) -> f32
pub fn to_f32(self, scale: i32) -> f32
Convert into f32.
Examples:
use primitive_fixed_point_decimal::{OobScaleFpdec, fpdec};
type Decimal = OobScaleFpdec<i32>;
let dec: Decimal = fpdec!(1.234, 4);
assert_eq!(dec.to_f32(4), 1.234);
let dec: Decimal = fpdec!(1234000, -3);
assert_eq!(dec.to_f32(-3), 1234000.0);Sourcepub fn to_f64(self, scale: i32) -> f64
pub fn to_f64(self, scale: i32) -> f64
Convert into f64.
Examples:
use primitive_fixed_point_decimal::{OobScaleFpdec, fpdec};
type Decimal = OobScaleFpdec<i32>;
let dec: Decimal = fpdec!(1.234, 4);
assert_eq!(dec.to_f64(4), 1.234);
let dec: Decimal = fpdec!(1234000, -3);
assert_eq!(dec.to_f64(-3), 1234000.0);Source§impl<I> OobScaleFpdec<I>where
I: FpdecInner + Signed,
impl<I> OobScaleFpdec<I>where
I: FpdecInner + Signed,
Sourcepub fn abs(self) -> Self
pub 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.
Sourcepub fn checked_abs(self) -> Option<Self>
pub fn checked_abs(self) -> Option<Self>
Checked absolute value. Computes self.abs(), returning None if self == MIN.
Trait Implementations§
Source§impl<I> Add for OobScaleFpdec<I>where
I: FpdecInner,
impl<I> Add for OobScaleFpdec<I>where
I: FpdecInner,
Source§impl<I> AddAssign for OobScaleFpdec<I>where
I: FpdecInner,
impl<I> AddAssign for OobScaleFpdec<I>where
I: FpdecInner,
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moreSource§impl<I: Clone> Clone for OobScaleFpdec<I>
impl<I: Clone> Clone for OobScaleFpdec<I>
Source§fn clone(&self) -> OobScaleFpdec<I>
fn clone(&self) -> OobScaleFpdec<I>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<I: Debug> Debug for OobScaleFpdec<I>
impl<I: Debug> Debug for OobScaleFpdec<I>
Source§impl<I: Default> Default for OobScaleFpdec<I>
impl<I: Default> Default for OobScaleFpdec<I>
Source§fn default() -> OobScaleFpdec<I>
fn default() -> OobScaleFpdec<I>
Source§impl<I, J, const S: i32> Div<ConstScaleFpdec<J, S>> for OobScaleFpdec<I>where
I: FpdecInner,
J: FpdecInner,
Performs the / operation with ConstScaleFpdec.
impl<I, J, const S: i32> Div<ConstScaleFpdec<J, S>> for OobScaleFpdec<I>where
I: FpdecInner,
J: FpdecInner,
Performs the / operation with ConstScaleFpdec.
§Panics
If Self::checked_div_const_scale returns None.
Source§type Output = OobScaleFpdec<I>
type Output = OobScaleFpdec<I>
/ operator.Source§impl<I, J> Div<J> for OobScaleFpdec<I>
Performs the / operation with an integer.
impl<I, J> Div<J> for OobScaleFpdec<I>
Performs the / operation with an integer.
§Panics
If Self::checked_div_int returns None.
Source§impl<I, J, const S: i32> DivAssign<ConstScaleFpdec<J, S>> for OobScaleFpdec<I>where
I: FpdecInner,
J: FpdecInner,
impl<I, J, const S: i32> DivAssign<ConstScaleFpdec<J, S>> for OobScaleFpdec<I>where
I: FpdecInner,
J: FpdecInner,
Source§fn div_assign(&mut self, rhs: ConstScaleFpdec<J, S>)
fn div_assign(&mut self, rhs: ConstScaleFpdec<J, S>)
/= operation. Read moreSource§impl<I, J> DivAssign<J> for OobScaleFpdec<I>
impl<I, J> DivAssign<J> for OobScaleFpdec<I>
Source§fn div_assign(&mut self, rhs: J)
fn div_assign(&mut self, rhs: J)
/= operation. Read moreSource§impl<I, const S: i32> From<ConstScaleFpdec<I, S>> for OobScaleFpdec<I>where
I: FpdecInner,
impl<I, const S: i32> From<ConstScaleFpdec<I, S>> for OobScaleFpdec<I>where
I: FpdecInner,
Source§fn from(sd: ConstScaleFpdec<I, S>) -> Self
fn from(sd: ConstScaleFpdec<I, S>) -> Self
Convert from ConstScaleFpdec to OobScaleFpdec with scale S.
Examples:
use primitive_fixed_point_decimal::{ConstScaleFpdec, OobScaleFpdec, fpdec};
type ConstDec = ConstScaleFpdec<i32, 6>;
type OobDec = OobScaleFpdec<i32>; // the OOB scale is 6 too
let sd: ConstDec = fpdec!(123.45);
let od: OobDec = sd.into(); // `od` has the same scale=6
assert_eq!(od, fpdec!(123.45, 6));Source§impl<I, const S: i32> From<OobScaleFpdec<I>> for ConstScaleFpdec<I, S>where
I: FpdecInner,
impl<I, const S: i32> From<OobScaleFpdec<I>> for ConstScaleFpdec<I, S>where
I: FpdecInner,
Source§fn from(od: OobScaleFpdec<I>) -> Self
fn from(od: OobScaleFpdec<I>) -> Self
Convert from OobScaleFpdec with scale S to ConstScaleFpdec.
Examples:
use primitive_fixed_point_decimal::{ConstScaleFpdec, OobScaleFpdec, fpdec};
type ConstDec = ConstScaleFpdec<i32, 6>;
type OobDec = OobScaleFpdec<i32>; // the OOB scale is 6 too
let od: OobDec = fpdec!(123.45, 6); // make sure that `od` has the same scale=6
let sd: ConstDec = od.into();
assert_eq!(sd, fpdec!(123.45));Source§impl<I: Hash> Hash for OobScaleFpdec<I>
impl<I: Hash> Hash for OobScaleFpdec<I>
Source§impl<I, J> IntoRatioInt<J> for OobScaleFpdec<I>where
I: FpdecInner + Into<J>,
impl<I, J> IntoRatioInt<J> for OobScaleFpdec<I>where
I: FpdecInner + Into<J>,
Source§impl<I, J, const S: i32> Mul<ConstScaleFpdec<J, S>> for OobScaleFpdec<I>where
I: FpdecInner,
J: FpdecInner,
Performs the * operation with ConstScaleFpdec.
impl<I, J, const S: i32> Mul<ConstScaleFpdec<J, S>> for OobScaleFpdec<I>where
I: FpdecInner,
J: FpdecInner,
Performs the * operation with ConstScaleFpdec.
§Panics
If Self::checked_mul_const_scale returns None.
Source§type Output = OobScaleFpdec<I>
type Output = OobScaleFpdec<I>
* operator.Source§impl<I, J> Mul<J> for OobScaleFpdec<I>
Performs the * operation with an integer.
impl<I, J> Mul<J> for OobScaleFpdec<I>
Performs the * operation with an integer.
§Panics
If Self::checked_mul_int returns None.
Source§impl<I, J, const S: i32> MulAssign<ConstScaleFpdec<J, S>> for OobScaleFpdec<I>where
I: FpdecInner,
J: FpdecInner,
impl<I, J, const S: i32> MulAssign<ConstScaleFpdec<J, S>> for OobScaleFpdec<I>where
I: FpdecInner,
J: FpdecInner,
Source§fn mul_assign(&mut self, rhs: ConstScaleFpdec<J, S>)
fn mul_assign(&mut self, rhs: ConstScaleFpdec<J, S>)
*= operation. Read moreSource§impl<I, J> MulAssign<J> for OobScaleFpdec<I>
impl<I, J> MulAssign<J> for OobScaleFpdec<I>
Source§fn mul_assign(&mut self, rhs: J)
fn mul_assign(&mut self, rhs: J)
*= operation. Read moreSource§impl<I> Neg for OobScaleFpdec<I>where
I: FpdecInner + Signed,
impl<I> Neg for OobScaleFpdec<I>where
I: FpdecInner + Signed,
Source§impl<I: Ord> Ord for OobScaleFpdec<I>
impl<I: Ord> Ord for OobScaleFpdec<I>
Source§fn cmp(&self, other: &OobScaleFpdec<I>) -> Ordering
fn cmp(&self, other: &OobScaleFpdec<I>) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<I: PartialEq> PartialEq for OobScaleFpdec<I>
impl<I: PartialEq> PartialEq for OobScaleFpdec<I>
Source§impl<I: PartialOrd> PartialOrd for OobScaleFpdec<I>
impl<I: PartialOrd> PartialOrd for OobScaleFpdec<I>
Source§impl<I> Sub for OobScaleFpdec<I>where
I: FpdecInner,
impl<I> Sub for OobScaleFpdec<I>where
I: FpdecInner,
Source§impl<I> SubAssign for OobScaleFpdec<I>where
I: FpdecInner,
impl<I> SubAssign for OobScaleFpdec<I>where
I: FpdecInner,
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-= operation. Read moreSource§impl<I> TryFrom<(f32, i32)> for OobScaleFpdec<I>where
I: FromPrimitive + FpdecInner,
impl<I> TryFrom<(f32, i32)> for OobScaleFpdec<I>where
I: FromPrimitive + FpdecInner,
Source§fn try_from(t: (f32, i32)) -> Result<Self, Self::Error>
fn try_from(t: (f32, i32)) -> Result<Self, Self::Error>
Convert from float and scale. Returning error if overflow occurred.
Since it’s hard for the float types to represent decimal fraction exactly, so this method always rounds the float number into OobScaleFpdec.
Examples:
use core::str::FromStr;
use primitive_fixed_point_decimal::{OobScaleFpdec, ParseError};
type Decimal = OobScaleFpdec<i32>;
assert_eq!(Decimal::try_from((1.23, 4)).unwrap(), Decimal::try_from_str("1.23", 4).unwrap());
assert_eq!(Decimal::try_from((1.23456789, 4)).unwrap(), Decimal::try_from_str("1.2346", 4).unwrap());Source§type Error = ParseError
type Error = ParseError
Source§impl<I> TryFrom<(f64, i32)> for OobScaleFpdec<I>where
I: FromPrimitive + FpdecInner,
impl<I> TryFrom<(f64, i32)> for OobScaleFpdec<I>where
I: FromPrimitive + FpdecInner,
Source§fn try_from(t: (f64, i32)) -> Result<Self, Self::Error>
fn try_from(t: (f64, i32)) -> Result<Self, Self::Error>
Convert from float and scale. Returning error if overflow occurred.
Since it’s hard for the float types to represent decimal fraction exactly, so this method always rounds the float number into OobScaleFpdec.
Examples:
use core::str::FromStr;
use primitive_fixed_point_decimal::{OobScaleFpdec, ParseError};
type Decimal = OobScaleFpdec<i32>;
assert_eq!(Decimal::try_from((1.23, 4)).unwrap(), Decimal::try_from_str("1.23", 4).unwrap());
assert_eq!(Decimal::try_from((1.23456789, 4)).unwrap(), Decimal::try_from_str("1.2346", 4).unwrap());Source§type Error = ParseError
type Error = ParseError
Source§impl<I> TryFrom<(i128, i32)> for OobScaleFpdec<I>where
I: FpdecInner,
impl<I> TryFrom<(i128, i32)> for OobScaleFpdec<I>where
I: FpdecInner,
Source§fn try_from(i: (i128, i32)) -> Result<Self, Self::Error>
fn try_from(i: (i128, i32)) -> Result<Self, Self::Error>
Convert from integer with scale. Returning error if
overflow occurred or lossing precision under scale < 0.
Examples:
use core::str::FromStr;
use primitive_fixed_point_decimal::{OobScaleFpdec, ParseError};
type Decimal = OobScaleFpdec<i32>;
assert_eq!(Decimal::try_from((123, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((123_i8, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((120000000000_i64, -10)).unwrap(), Decimal::try_from_str("120000000000", -10).unwrap());
assert_eq!(Decimal::try_from((9999999, 4)), Err(ParseError::Overflow));
assert_eq!(Decimal::try_from((123, -4)), Err(ParseError::Precision));Source§type Error = ParseError
type Error = ParseError
Source§impl<I> TryFrom<(i16, i32)> for OobScaleFpdec<I>where
I: FpdecInner,
impl<I> TryFrom<(i16, i32)> for OobScaleFpdec<I>where
I: FpdecInner,
Source§fn try_from(i: (i16, i32)) -> Result<Self, Self::Error>
fn try_from(i: (i16, i32)) -> Result<Self, Self::Error>
Convert from integer with scale. Returning error if
overflow occurred or lossing precision under scale < 0.
Examples:
use core::str::FromStr;
use primitive_fixed_point_decimal::{OobScaleFpdec, ParseError};
type Decimal = OobScaleFpdec<i32>;
assert_eq!(Decimal::try_from((123, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((123_i8, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((120000000000_i64, -10)).unwrap(), Decimal::try_from_str("120000000000", -10).unwrap());
assert_eq!(Decimal::try_from((9999999, 4)), Err(ParseError::Overflow));
assert_eq!(Decimal::try_from((123, -4)), Err(ParseError::Precision));Source§type Error = ParseError
type Error = ParseError
Source§impl<I> TryFrom<(i32, i32)> for OobScaleFpdec<I>where
I: FpdecInner,
impl<I> TryFrom<(i32, i32)> for OobScaleFpdec<I>where
I: FpdecInner,
Source§fn try_from(i: (i32, i32)) -> Result<Self, Self::Error>
fn try_from(i: (i32, i32)) -> Result<Self, Self::Error>
Convert from integer with scale. Returning error if
overflow occurred or lossing precision under scale < 0.
Examples:
use core::str::FromStr;
use primitive_fixed_point_decimal::{OobScaleFpdec, ParseError};
type Decimal = OobScaleFpdec<i32>;
assert_eq!(Decimal::try_from((123, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((123_i8, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((120000000000_i64, -10)).unwrap(), Decimal::try_from_str("120000000000", -10).unwrap());
assert_eq!(Decimal::try_from((9999999, 4)), Err(ParseError::Overflow));
assert_eq!(Decimal::try_from((123, -4)), Err(ParseError::Precision));Source§type Error = ParseError
type Error = ParseError
Source§impl<I> TryFrom<(i64, i32)> for OobScaleFpdec<I>where
I: FpdecInner,
impl<I> TryFrom<(i64, i32)> for OobScaleFpdec<I>where
I: FpdecInner,
Source§fn try_from(i: (i64, i32)) -> Result<Self, Self::Error>
fn try_from(i: (i64, i32)) -> Result<Self, Self::Error>
Convert from integer with scale. Returning error if
overflow occurred or lossing precision under scale < 0.
Examples:
use core::str::FromStr;
use primitive_fixed_point_decimal::{OobScaleFpdec, ParseError};
type Decimal = OobScaleFpdec<i32>;
assert_eq!(Decimal::try_from((123, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((123_i8, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((120000000000_i64, -10)).unwrap(), Decimal::try_from_str("120000000000", -10).unwrap());
assert_eq!(Decimal::try_from((9999999, 4)), Err(ParseError::Overflow));
assert_eq!(Decimal::try_from((123, -4)), Err(ParseError::Precision));Source§type Error = ParseError
type Error = ParseError
Source§impl<I> TryFrom<(i8, i32)> for OobScaleFpdec<I>where
I: FpdecInner,
impl<I> TryFrom<(i8, i32)> for OobScaleFpdec<I>where
I: FpdecInner,
Source§fn try_from(i: (i8, i32)) -> Result<Self, Self::Error>
fn try_from(i: (i8, i32)) -> Result<Self, Self::Error>
Convert from integer with scale. Returning error if
overflow occurred or lossing precision under scale < 0.
Examples:
use core::str::FromStr;
use primitive_fixed_point_decimal::{OobScaleFpdec, ParseError};
type Decimal = OobScaleFpdec<i32>;
assert_eq!(Decimal::try_from((123, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((123_i8, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((120000000000_i64, -10)).unwrap(), Decimal::try_from_str("120000000000", -10).unwrap());
assert_eq!(Decimal::try_from((9999999, 4)), Err(ParseError::Overflow));
assert_eq!(Decimal::try_from((123, -4)), Err(ParseError::Precision));Source§type Error = ParseError
type Error = ParseError
Source§impl<I> TryFrom<(u128, i32)> for OobScaleFpdec<I>where
I: FpdecInner,
impl<I> TryFrom<(u128, i32)> for OobScaleFpdec<I>where
I: FpdecInner,
Source§fn try_from(i: (u128, i32)) -> Result<Self, Self::Error>
fn try_from(i: (u128, i32)) -> Result<Self, Self::Error>
Convert from integer with scale. Returning error if
overflow occurred or lossing precision under scale < 0.
Examples:
use core::str::FromStr;
use primitive_fixed_point_decimal::{OobScaleFpdec, ParseError};
type Decimal = OobScaleFpdec<i32>;
assert_eq!(Decimal::try_from((123, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((123_i8, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((120000000000_i64, -10)).unwrap(), Decimal::try_from_str("120000000000", -10).unwrap());
assert_eq!(Decimal::try_from((9999999, 4)), Err(ParseError::Overflow));
assert_eq!(Decimal::try_from((123, -4)), Err(ParseError::Precision));Source§type Error = ParseError
type Error = ParseError
Source§impl<I> TryFrom<(u16, i32)> for OobScaleFpdec<I>where
I: FpdecInner,
impl<I> TryFrom<(u16, i32)> for OobScaleFpdec<I>where
I: FpdecInner,
Source§fn try_from(i: (u16, i32)) -> Result<Self, Self::Error>
fn try_from(i: (u16, i32)) -> Result<Self, Self::Error>
Convert from integer with scale. Returning error if
overflow occurred or lossing precision under scale < 0.
Examples:
use core::str::FromStr;
use primitive_fixed_point_decimal::{OobScaleFpdec, ParseError};
type Decimal = OobScaleFpdec<i32>;
assert_eq!(Decimal::try_from((123, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((123_i8, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((120000000000_i64, -10)).unwrap(), Decimal::try_from_str("120000000000", -10).unwrap());
assert_eq!(Decimal::try_from((9999999, 4)), Err(ParseError::Overflow));
assert_eq!(Decimal::try_from((123, -4)), Err(ParseError::Precision));Source§type Error = ParseError
type Error = ParseError
Source§impl<I> TryFrom<(u32, i32)> for OobScaleFpdec<I>where
I: FpdecInner,
impl<I> TryFrom<(u32, i32)> for OobScaleFpdec<I>where
I: FpdecInner,
Source§fn try_from(i: (u32, i32)) -> Result<Self, Self::Error>
fn try_from(i: (u32, i32)) -> Result<Self, Self::Error>
Convert from integer with scale. Returning error if
overflow occurred or lossing precision under scale < 0.
Examples:
use core::str::FromStr;
use primitive_fixed_point_decimal::{OobScaleFpdec, ParseError};
type Decimal = OobScaleFpdec<i32>;
assert_eq!(Decimal::try_from((123, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((123_i8, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((120000000000_i64, -10)).unwrap(), Decimal::try_from_str("120000000000", -10).unwrap());
assert_eq!(Decimal::try_from((9999999, 4)), Err(ParseError::Overflow));
assert_eq!(Decimal::try_from((123, -4)), Err(ParseError::Precision));Source§type Error = ParseError
type Error = ParseError
Source§impl<I> TryFrom<(u64, i32)> for OobScaleFpdec<I>where
I: FpdecInner,
impl<I> TryFrom<(u64, i32)> for OobScaleFpdec<I>where
I: FpdecInner,
Source§fn try_from(i: (u64, i32)) -> Result<Self, Self::Error>
fn try_from(i: (u64, i32)) -> Result<Self, Self::Error>
Convert from integer with scale. Returning error if
overflow occurred or lossing precision under scale < 0.
Examples:
use core::str::FromStr;
use primitive_fixed_point_decimal::{OobScaleFpdec, ParseError};
type Decimal = OobScaleFpdec<i32>;
assert_eq!(Decimal::try_from((123, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((123_i8, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((120000000000_i64, -10)).unwrap(), Decimal::try_from_str("120000000000", -10).unwrap());
assert_eq!(Decimal::try_from((9999999, 4)), Err(ParseError::Overflow));
assert_eq!(Decimal::try_from((123, -4)), Err(ParseError::Precision));Source§type Error = ParseError
type Error = ParseError
Source§impl<I> TryFrom<(u8, i32)> for OobScaleFpdec<I>where
I: FpdecInner,
impl<I> TryFrom<(u8, i32)> for OobScaleFpdec<I>where
I: FpdecInner,
Source§fn try_from(i: (u8, i32)) -> Result<Self, Self::Error>
fn try_from(i: (u8, i32)) -> Result<Self, Self::Error>
Convert from integer with scale. Returning error if
overflow occurred or lossing precision under scale < 0.
Examples:
use core::str::FromStr;
use primitive_fixed_point_decimal::{OobScaleFpdec, ParseError};
type Decimal = OobScaleFpdec<i32>;
assert_eq!(Decimal::try_from((123, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((123_i8, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((120000000000_i64, -10)).unwrap(), Decimal::try_from_str("120000000000", -10).unwrap());
assert_eq!(Decimal::try_from((9999999, 4)), Err(ParseError::Overflow));
assert_eq!(Decimal::try_from((123, -4)), Err(ParseError::Precision));