Skip to content

Commit 4e688ba

Browse files
committed
Add to_uint_floor, to_uint_ceil
1 parent 9ee304c commit 4e688ba

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

packages/std/src/math/decimal.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,54 @@ impl Decimal {
357357
Err(_) => Self::MAX,
358358
}
359359
}
360+
361+
/// Converts this decimal to an unsigned integer by truncating
362+
/// the fractional part, e.g. 22.5 becomes 22.
363+
///
364+
/// ## Examples
365+
///
366+
/// ```
367+
/// use std::str::FromStr;
368+
/// use cosmwasm_std::{Decimal, Uint128};
369+
///
370+
/// let d = Decimal::from_str("12.345").unwrap();
371+
/// assert_eq!(d.to_uint_floor(), Uint128::new(12));
372+
///
373+
/// let d = Decimal::from_str("12.999").unwrap();
374+
/// assert_eq!(d.to_uint_floor(), Uint128::new(12));
375+
///
376+
/// let d = Decimal::from_str("75.0").unwrap();
377+
/// assert_eq!(d.to_uint_floor(), Uint128::new(75));
378+
/// ```
379+
pub fn to_uint_floor(self) -> Uint128 {
380+
self.0 / Self::DECIMAL_FRACTIONAL
381+
}
382+
383+
/// Converts this decimal to an unsigned integer by rounting up
384+
/// to the next integer, e.g. 22.3 becomes 23.
385+
///
386+
/// ## Examples
387+
///
388+
/// ```
389+
/// use std::str::FromStr;
390+
/// use cosmwasm_std::{Decimal, Uint128};
391+
///
392+
/// let d = Decimal::from_str("12.345").unwrap();
393+
/// assert_eq!(d.to_uint_ceil(), Uint128::new(13));
394+
///
395+
/// let d = Decimal::from_str("12.999").unwrap();
396+
/// assert_eq!(d.to_uint_ceil(), Uint128::new(13));
397+
///
398+
/// let d = Decimal::from_str("75.0").unwrap();
399+
/// assert_eq!(d.to_uint_ceil(), Uint128::new(75));
400+
/// ```
401+
pub fn to_uint_ceil(self) -> Uint128 {
402+
if (self.0 % Self::DECIMAL_FRACTIONAL).is_zero() {
403+
self.0 / Self::DECIMAL_FRACTIONAL
404+
} else {
405+
self.0 / Self::DECIMAL_FRACTIONAL + Uint128::one()
406+
}
407+
}
360408
}
361409

362410
impl Fraction<Uint128> for Decimal {

packages/std/src/math/decimal256.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,54 @@ impl Decimal256 {
374374
Err(_) => Self::MAX,
375375
}
376376
}
377+
378+
/// Converts this decimal to an unsigned integer by truncating
379+
/// the fractional part, e.g. 22.5 becomes 22.
380+
///
381+
/// ## Examples
382+
///
383+
/// ```
384+
/// use std::str::FromStr;
385+
/// use cosmwasm_std::{Decimal256, Uint256};
386+
///
387+
/// let d = Decimal256::from_str("12.345").unwrap();
388+
/// assert_eq!(d.to_uint_floor(), Uint256::from(12u64));
389+
///
390+
/// let d = Decimal256::from_str("12.999").unwrap();
391+
/// assert_eq!(d.to_uint_floor(), Uint256::from(12u64));
392+
///
393+
/// let d = Decimal256::from_str("75.0").unwrap();
394+
/// assert_eq!(d.to_uint_floor(), Uint256::from(75u64));
395+
/// ```
396+
pub fn to_uint_floor(self) -> Uint256 {
397+
self.0 / Self::DECIMAL_FRACTIONAL
398+
}
399+
400+
/// Converts this decimal to an unsigned integer by rounting up
401+
/// to the next integer, e.g. 22.3 becomes 23.
402+
///
403+
/// ## Examples
404+
///
405+
/// ```
406+
/// use std::str::FromStr;
407+
/// use cosmwasm_std::{Decimal256, Uint256};
408+
///
409+
/// let d = Decimal256::from_str("12.345").unwrap();
410+
/// assert_eq!(d.to_uint_ceil(), Uint256::from(13u64));
411+
///
412+
/// let d = Decimal256::from_str("12.999").unwrap();
413+
/// assert_eq!(d.to_uint_ceil(), Uint256::from(13u64));
414+
///
415+
/// let d = Decimal256::from_str("75.0").unwrap();
416+
/// assert_eq!(d.to_uint_ceil(), Uint256::from(75u64));
417+
/// ```
418+
pub fn to_uint_ceil(self) -> Uint256 {
419+
if (self.0 % Self::DECIMAL_FRACTIONAL).is_zero() {
420+
self.0 / Self::DECIMAL_FRACTIONAL
421+
} else {
422+
self.0 / Self::DECIMAL_FRACTIONAL + Uint256::one()
423+
}
424+
}
377425
}
378426

379427
impl Fraction<Uint256> for Decimal256 {

0 commit comments

Comments
 (0)