Skip to content

Commit

Permalink
Add trim_end_if_integer to FixedDecimal
Browse files Browse the repository at this point in the history
  • Loading branch information
sffc committed Dec 14, 2024
1 parent 6f10eaa commit 3a848ae
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions utils/fixed_decimal/src/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,66 @@ impl UnsignedFixedDecimal {
self.check_invariants();
}

/// Returns this number with its trailing zeros removed,
/// but only if the number is an integer
///
/// # Examples
///
/// ```
/// use fixed_decimal::UnsignedFixedDecimal;
///
/// let dec = UnsignedFixedDecimal::from(12340000u32)
/// .multiplied_pow10(-2);
/// assert_eq!("123400.00", dec.to_string());
/// assert_eq!("123400", dec.trimmed_end_if_integer().to_string());
///
/// // No effect if there are nonzero fractional digits:
/// let dec = UnsignedFixedDecimal::from(123400u32)
/// .multiplied_pow10(-4)
/// .padded_start(4);
/// assert_eq!("0012.3400", dec.to_string());
/// assert_eq!("0012.3400", dec.trimmed_end_if_integer().to_string());
/// ```
pub fn trimmed_end_if_integer(mut self) -> Self {
self.trim_end_if_integer();
self
}

/// Removes the trailing zeros of this number,
/// but only if the number is an integer
///
/// # Examples
///
/// ```
/// use fixed_decimal::UnsignedFixedDecimal;
///
/// let mut dec = UnsignedFixedDecimal::from(12340000u32)
/// .multiplied_pow10(-2);
/// assert_eq!("123400.00", dec.to_string());
///
/// dec.trim_end_if_integer();
/// assert_eq!("123400", dec.to_string());
///
/// // No effect on trailing zeros in the integer:
/// dec.trim_end_if_integer();
/// assert_eq!("123400", dec.to_string());
///
/// // No effect if there are nonzero fractional digits:
/// dec.multiply_pow10(-4);
/// dec.pad_start(4);
/// assert_eq!("0012.3400", dec.to_string());
///
/// dec.trim_end_if_integer();
/// assert_eq!("0012.3400", dec.to_string());
/// ```
pub fn trim_end_if_integer(&mut self) {
if self.nonzero_magnitude_end() >= 0 {
self.lower_magnitude = 0;
}
#[cfg(debug_assertions)]
self.check_invariants();
}

/// Returns this number padded with leading zeros on a particular position.
///
/// Negative position numbers have no effect.
Expand Down

0 comments on commit 3a848ae

Please sign in to comment.