Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debug impl for Decimal/Decimal256 uses decimal output #1600

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion packages/std/src/math/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use super::{Uint128, Uint256};
/// A fixed-point decimal value with 18 fractional digits, i.e. Decimal(1_000_000_000_000_000_000) == 1.0
///
/// The greatest possible value that can be represented is 340282366920938463463.374607431768211455 (which is (2^128 - 1) / 10^18)
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, JsonSchema)]
#[derive(Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, JsonSchema)]
pub struct Decimal(#[schemars(with = "String")] Uint128);

#[derive(Error, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -457,6 +457,12 @@ impl fmt::Display for Decimal {
}
}

impl fmt::Debug for Decimal {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Decimal({})", self)
}
}

impl Add for Decimal {
type Output = Self;

Expand Down Expand Up @@ -2015,4 +2021,14 @@ mod tests {
assert_eq!(&lhs == &rhs, expected);
}
}

#[test]
fn decimal_implements_debug() {
let test_cases = ["5", "5.01", "42", "0", "2"];

for s in test_cases {
let decimal = Decimal::from_str(s).unwrap();
assert_eq!(format!("Decimal({})", s), format!("{:?}", decimal));
}
}
}
18 changes: 17 additions & 1 deletion packages/std/src/math/decimal256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use super::Uint256;
/// The greatest possible value that can be represented is
/// 115792089237316195423570985008687907853269984665640564039457.584007913129639935
/// (which is (2^256 - 1) / 10^18)
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, JsonSchema)]
#[derive(Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, JsonSchema)]
pub struct Decimal256(#[schemars(with = "String")] Uint256);

#[derive(Error, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -482,6 +482,12 @@ impl fmt::Display for Decimal256 {
}
}

impl fmt::Debug for Decimal256 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Decimal256({})", self)
}
}

impl Add for Decimal256 {
type Output = Self;

Expand Down Expand Up @@ -2162,4 +2168,14 @@ mod tests {
assert_eq!(&lhs == &rhs, expected);
}
}

#[test]
fn decimal256_implements_debug() {
let test_cases = ["5", "5.01", "42", "0", "2"];

for s in test_cases {
let decimal256 = Decimal256::from_str(s).unwrap();
assert_eq!(format!("Decimal256({})", s), format!("{:?}", decimal256));
}
}
}