Skip to content

Commit 401b6fb

Browse files
snoybergwebmaster128
authored andcommitted
Debug impl for Decimal256 uses decimal output.
1 parent 1ddba30 commit 401b6fb

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

packages/std/src/math/decimal.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use super::{Uint128, Uint256};
1919
/// A fixed-point decimal value with 18 fractional digits, i.e. Decimal(1_000_000_000_000_000_000) == 1.0
2020
///
2121
/// The greatest possible value that can be represented is 340282366920938463463.374607431768211455 (which is (2^128 - 1) / 10^18)
22-
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, JsonSchema)]
22+
#[derive(Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, JsonSchema)]
2323
pub struct Decimal(#[schemars(with = "String")] Uint128);
2424

2525
#[derive(Error, Debug, PartialEq, Eq)]
@@ -457,6 +457,12 @@ impl fmt::Display for Decimal {
457457
}
458458
}
459459

460+
impl fmt::Debug for Decimal {
461+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
462+
write!(f, "{}", self)
463+
}
464+
}
465+
460466
impl Add for Decimal {
461467
type Output = Self;
462468

@@ -2015,4 +2021,14 @@ mod tests {
20152021
assert_eq!(&lhs == &rhs, expected);
20162022
}
20172023
}
2024+
2025+
#[test]
2026+
fn decimal_implements_debug() {
2027+
let test_cases = ["5", "5.01", "42", "0", "2"];
2028+
2029+
for s in test_cases {
2030+
let decimal = Decimal::from_str(s).unwrap();
2031+
assert_eq!(s, format!("{:?}", decimal));
2032+
}
2033+
}
20182034
}

packages/std/src/math/decimal256.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use super::Uint256;
2222
/// The greatest possible value that can be represented is
2323
/// 115792089237316195423570985008687907853269984665640564039457.584007913129639935
2424
/// (which is (2^256 - 1) / 10^18)
25-
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, JsonSchema)]
25+
#[derive(Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, JsonSchema)]
2626
pub struct Decimal256(#[schemars(with = "String")] Uint256);
2727

2828
#[derive(Error, Debug, PartialEq, Eq)]
@@ -482,6 +482,12 @@ impl fmt::Display for Decimal256 {
482482
}
483483
}
484484

485+
impl fmt::Debug for Decimal256 {
486+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
487+
write!(f, "{}", self)
488+
}
489+
}
490+
485491
impl Add for Decimal256 {
486492
type Output = Self;
487493

@@ -2162,4 +2168,14 @@ mod tests {
21622168
assert_eq!(&lhs == &rhs, expected);
21632169
}
21642170
}
2171+
2172+
#[test]
2173+
fn decimal256_implements_debug() {
2174+
let test_cases = ["5", "5.01", "42", "0", "2"];
2175+
2176+
for s in test_cases {
2177+
let decimal256 = Decimal256::from_str(s).unwrap();
2178+
assert_eq!(s, format!("{:?}", decimal256));
2179+
}
2180+
}
21652181
}

0 commit comments

Comments
 (0)