Skip to content

Commit

Permalink
Merge branch 'master' into feature/docs
Browse files Browse the repository at this point in the history
  • Loading branch information
paupino committed Apr 16, 2021
2 parents a16e060 + 5faeb95 commit 432aede
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,26 @@ impl Decimal {
((self.flags & SCALE_MASK) >> SCALE_SHIFT) as u32
}

/// Returns the mantissa of the decimal number.
///
/// # Example
///
/// ```
/// use rust_decimal::prelude::*;
///
/// let num = Decimal::from_str("-1.2345678").unwrap();
/// assert_eq!(num.mantissa(), -12345678i128);
/// assert_eq!(num.scale(), 7);
/// ```
pub const fn mantissa(&self) -> i128 {
let raw = (self.lo as i128) | ((self.mid as i128) << 32) | ((self.hi as i128) << 64);
if self.is_sign_negative() {
-raw
} else {
raw
}
}

/// An optimized method for changing the sign of a decimal number.
///
/// # Arguments
Expand Down
14 changes: 14 additions & 0 deletions tests/decimal_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ macro_rules! either {
};
}

#[test]
fn it_can_extract_the_mantissa() {
let tests = [
("1", 1i128, 0),
("1.123456", 1123456i128, 6),
("-0.123456", -123456i128, 6),
];
for &(input, mantissa, scale) in &tests {
let num = Decimal::from_str(input).unwrap();
assert_eq!(num.mantissa(), mantissa, "Mantissa for {}", input);
assert_eq!(num.scale(), scale, "Scale for {}", input);
}
}

// Parsing

#[test]
Expand Down

0 comments on commit 432aede

Please sign in to comment.