Skip to content

Commit

Permalink
Add trim_end_if_integer to FixedDecimal (unicode-org#5903)
Browse files Browse the repository at this point in the history
  • Loading branch information
sffc authored Dec 16, 2024
1 parent 6f10eaa commit d845fb6
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ffi/capi/bindings/c/SignedFixedDecimal.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions ffi/capi/bindings/cpp/icu4x/SignedFixedDecimal.d.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions ffi/capi/bindings/cpp/icu4x/SignedFixedDecimal.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions ffi/capi/bindings/dart/SignedFixedDecimal.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions ffi/capi/bindings/js/SignedFixedDecimal.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions ffi/capi/bindings/js/SignedFixedDecimal.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions ffi/capi/src/fixed_decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,16 @@ pub mod ffi {
self.0.absolute.trim_end()
}

#[diplomat::rust_link(fixed_decimal::UnsignedFixedDecimal::trim_end_if_integer, FnInStruct)]
#[diplomat::rust_link(
fixed_decimal::UnsignedFixedDecimal::trimmed_end_if_integer,
FnInStruct,
hidden
)]
pub fn trim_end_if_integer(&mut self) {
self.0.absolute.trim_end_if_integer()
}

/// Zero-pad the [`SignedFixedDecimal`] on the left to a particular position
#[diplomat::rust_link(fixed_decimal::FixedDecimal::pad_start, FnInStruct)]
#[diplomat::rust_link(fixed_decimal::FixedDecimal::padded_start, FnInStruct, hidden)]
Expand Down
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 d845fb6

Please sign in to comment.