pub struct Decimal<I: UnderlyingInt>(/* private fields */);Expand description
The decimal type.
The I is the underlying integer type. It supports u128 only by now,
and will support u64 and u32 in next version.
They have aliases Dec128, Dec64 and Dec32 respectively.
Implementations§
Source§impl<I: UnderlyingInt> Decimal<I>
impl<I: UnderlyingInt> Decimal<I>
Sourcepub fn checked_add(self, right: Self) -> Option<Self>
pub fn checked_add(self, right: Self) -> Option<Self>
Computes the addition.
Return None if overflow.
There may loss precision if the scales of the 2 operands differ too greatly. It’s a classic round-off errors. of floating-point calculation.
§Examples:
use lean_decimal::Dec128;
let a = Dec128::from_parts(123, 2); // 1.23
let b = Dec128::from_parts(1, 4); // 0.0001
let sum = Dec128::from_parts(12301, 4); // 1.2301
assert_eq!(a.checked_add(b).unwrap(), sum);Sourcepub fn checked_sub(self, right: Self) -> Option<Self>
pub fn checked_sub(self, right: Self) -> Option<Self>
Computes the addition.
Return None if overflow.
There may loss precision if the scales of the 2 operands differ too greatly. It’s a classic round-off errors. of floating-point calculation.
§Examples:
use lean_decimal::Dec128;
let a = Dec128::from_parts(123, 2); // 1.23
let b = Dec128::from_parts(1, 4); // 0.0001
let diff = Dec128::from_parts(12299, 4); // 1.2299
assert_eq!(a.checked_sub(b).unwrap(), diff);Sourcepub fn checked_mul(self, right: impl Into<Self>) -> Option<Self>
pub fn checked_mul(self, right: impl Into<Self>) -> Option<Self>
Computes the multiplication.
Return None if overflow.
The right oprand could be short integers or decimals.
§Examples:
use lean_decimal::Dec128;
let a = Dec128::from_parts(123, 2); // 1.23
let b = Dec128::from_parts(1, 4); // 0.0001
let prod = Dec128::from_parts(123, 6); // 0.000123
assert_eq!(a.checked_mul(b).unwrap(), prod);
// by integer
let prod = Dec128::from_parts(246, 2); // 2.46
assert_eq!(a.checked_mul(2).unwrap(), prod);Sourcepub fn checked_div(self, right: impl Into<Self>) -> Option<Self>
pub fn checked_div(self, right: impl Into<Self>) -> Option<Self>
Computes the division.
Return None if overflow or divied by zero.
The right oprand could be short integers or decimals.
§Examples:
use lean_decimal::Dec128;
let a = Dec128::from_parts(123, 2); // 1.23
let b = Dec128::from_parts(1, 4); // 0.0001
let q = Dec128::from_parts(12300, 0); // 12300
assert_eq!(a.checked_div(b).unwrap(), q);
// by integer
let q = Dec128::from_parts(615, 3); // 0.615
assert_eq!(a.checked_div(2).unwrap(), q);Sourcepub fn round_to(self, dst_scale: u32) -> Self
pub fn round_to(self, dst_scale: u32) -> Self
Round to the @dst_scale. Keep @dst_scale fraction decimal fractions.
Do nothing if the original scale is not bigger than @dst_scale
§Examples:
use lean_decimal::Dec128;
let a = Dec128::from_parts(12345678, 6); // 12.345678
let b = Dec128::from_parts(1235, 2); // 12.35
assert_eq!(a.round_to(2), b);Source§impl<I: UnderlyingInt> Decimal<I>
impl<I: UnderlyingInt> Decimal<I>
Sourcepub fn is_positive(self) -> bool
pub fn is_positive(self) -> bool
Return if the number is positive.
Sourcepub fn is_negative(self) -> bool
pub fn is_negative(self) -> bool
Return if the number is negative.
Source§impl<I: UnderlyingInt> Decimal<I>
impl<I: UnderlyingInt> Decimal<I>
Sourcepub const MAX: Self
pub const MAX: Self
The largest value. To be largest, the scale is 0, so this is an
integer, 2^b - 1, where b is the mantissa bits, which is
121, 58, 27 for Dec128, Dec64 and Dec32 correspondingly.
Sourcepub fn parts(self) -> (I::Signed, u32)
pub fn parts(self) -> (I::Signed, u32)
Deconstruct the decimal into signed mantissa and scale.
§Examples:
use lean_decimal::Dec128;
use core::str::FromStr;
let d = Dec128::from_str("3.14").unwrap();
assert_eq!(d.parts(), (314, 2));Sourcepub fn from_parts<S>(mantissa: S, scale: u32) -> Self
pub fn from_parts<S>(mantissa: S, scale: u32) -> Self
Construct a decimal from signed mantissa and scale.
§Panic:
Panic if the mantissa or scale is out of range. Use Self::try_from_parts
for the checked version.
§Examples:
use lean_decimal::Dec128;
let d = Dec128::from_parts(314, 2);
assert_eq!(d.to_string(), "3.14");Sourcepub fn try_from_parts<S>(mantissa: S, scale: u32) -> Option<Self>
pub fn try_from_parts<S>(mantissa: S, scale: u32) -> Option<Self>
Construct a decimal from signed mantissa and scale.
Return None if the mantissa or scale is out of range.
§Examples:
use lean_decimal::Dec128;
let d = Dec128::try_from_parts(314, 2).unwrap();
assert_eq!(d.to_string(), "3.14");
let d = Dec128::try_from_parts(314, 99); // 99 is out of range
assert!(d.is_none());Sourcepub const fn underlying(self) -> I
pub const fn underlying(self) -> I
Return the underlying integer.
You should not try to decode this integer yourself. You should just hold this and load it later.
Sourcepub fn from_underlying(ui: I) -> Self
pub fn from_underlying(ui: I) -> Self
Load from underlying integer, which should only be got from Self::underlying.
§Panic:
Panic if invalid. Use Self::try_from_underlying for checked version.
Sourcepub fn try_from_underlying(ui: I) -> Option<Self>
pub fn try_from_underlying(ui: I) -> Option<Self>
Load from underlying integer, which should only be got from Self::underlying.
Sourcepub const unsafe fn unchecked_from_underlying(ui: I) -> Self
pub const unsafe fn unchecked_from_underlying(ui: I) -> Self
Load from underlying integer, which should only be got from Self::underlying.
SAFETY: It’s safe if you make sure the integer was got from Self::underlying.
Trait Implementations§
Source§impl<I: UnderlyingInt> Add for Decimal<I>
impl<I: UnderlyingInt> Add for Decimal<I>
Source§impl<I: UnderlyingInt> AddAssign for Decimal<I>
impl<I: UnderlyingInt> AddAssign for Decimal<I>
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moreSource§impl<I: UnderlyingInt> Debug for Decimal<I>
impl<I: UnderlyingInt> Debug for Decimal<I>
Source§impl<I: UnderlyingInt> Display for Decimal<I>
Display the decimal.
impl<I: UnderlyingInt> Display for Decimal<I>
Display the decimal.
It supports some formatting options: width, fill, alignment, precision, sign and 0-fill.
Examples:
use lean_decimal::Dec128;
let d = Dec128::from_parts(12_3470, 4);
assert_eq!(format!("{}", d), "12.3470");
assert_eq!(format!("{:.6}", d), "12.347000"); // set precision: pad 0
assert_eq!(format!("{:.2}", d), "12.35"); // set smaller precision: round the number
assert_eq!(format!("{:x>10}", d), "xxx12.3470"); // set width, fill, alignment
assert_eq!(format!("{:+}", d), "+12.3470"); // set signSource§impl<I: UnderlyingInt, R: Into<Self>> DivAssign<R> for Decimal<I>
impl<I: UnderlyingInt, R: Into<Self>> DivAssign<R> for Decimal<I>
Source§fn div_assign(&mut self, rhs: R)
fn div_assign(&mut self, rhs: R)
/= operation. Read moreSource§impl<I: UnderlyingInt, R: Into<Self>> MulAssign<R> for Decimal<I>
impl<I: UnderlyingInt, R: Into<Self>> MulAssign<R> for Decimal<I>
Source§fn mul_assign(&mut self, rhs: R)
fn mul_assign(&mut self, rhs: R)
*= operation. Read moreSource§impl<I: UnderlyingInt> Neg for Decimal<I>
impl<I: UnderlyingInt> Neg for Decimal<I>
Source§impl<I: UnderlyingInt> Ord for Decimal<I>
impl<I: UnderlyingInt> Ord for Decimal<I>
Source§impl<I: UnderlyingInt> PartialEq for Decimal<I>
impl<I: UnderlyingInt> PartialEq for Decimal<I>
Source§impl<I: UnderlyingInt> PartialOrd for Decimal<I>
impl<I: UnderlyingInt> PartialOrd for Decimal<I>
Source§impl<I: UnderlyingInt> Sub for Decimal<I>
impl<I: UnderlyingInt> Sub for Decimal<I>
Source§impl<I: UnderlyingInt> SubAssign for Decimal<I>
impl<I: UnderlyingInt> SubAssign for Decimal<I>
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-= operation. Read more