Skip to content

Commit

Permalink
Decimal rounding
Browse files Browse the repository at this point in the history
  • Loading branch information
Sajjon committed Feb 17, 2024
1 parent 5ec79b6 commit ff8a4a4
Show file tree
Hide file tree
Showing 10 changed files with 281 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@ extension Decimal192 {
public static let maxDivisibility: UInt = 18
}

// MARK: Truncation and rounding

extension Decimal192 {
/// Rounds to `decimalPlaces` decimals, in the direction of 0
public func floor(decimalPlaces: UInt) -> Self {
try! round(decimalPlaces: Int32(decimalPlaces), roundingMode: .toZero)
}

/// Rounds to `decimalPlaces` decimals, in the direction away from zero
public func ceil(decimalPlaces: UInt) -> Self {
try! round(decimalPlaces: Int32(decimalPlaces), roundingMode: .awayFromZero)
}

/// Rounds to `decimalPlaces` decimals
public func rounded(decimalPlaces: UInt = 0) -> Self {
try! round(decimalPlaces: Int32(decimalPlaces), roundingMode: .toNearestMidpointAwayFromZero)
}
}

extension Decimal192 {
public var clamped: Self {
isNegative() ? .zero : self
Expand Down
6 changes: 3 additions & 3 deletions src/core/types/bag_of_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,12 +346,12 @@ mod tests {
#[test]
fn default_is_empty() {
assert_eq!(BagOfBytes::default(), BagOfBytes::new());
assert_eq!(BagOfBytes::default().is_empty(), true);
assert!(BagOfBytes::default().is_empty());
}

#[test]
fn is_empty() {
assert_eq!(BagOfBytes::placeholder().is_empty(), false);
assert!(!BagOfBytes::placeholder().is_empty());
}

#[test]
Expand Down Expand Up @@ -421,7 +421,7 @@ mod tests {
#[test]
fn from_vec_roundtrip() {
let vec = Vec::from([0u8; 32]);
let sut: BagOfBytes = vec.clone().try_into().unwrap();
let sut: BagOfBytes = vec.clone().into();
assert_eq!(sut.to_vec(), vec);
}

Expand Down
Loading

0 comments on commit ff8a4a4

Please sign in to comment.