diff --git a/packages/std/src/math/uint128.rs b/packages/std/src/math/uint128.rs index abf5abae75..a475107c4f 100644 --- a/packages/std/src/math/uint128.rs +++ b/packages/std/src/math/uint128.rs @@ -517,6 +517,12 @@ mod tests { assert_eq!(a.to_string(), "0"); } + #[test] + fn uint128_display_padding_works() { + let a = Uint128::from(123u64); + assert_eq!(format!("Embedded: {:05}", a), "Embedded: 00123"); + } + #[test] fn uint128_is_zero_works() { assert!(Uint128::zero().is_zero()); diff --git a/packages/std/src/math/uint256.rs b/packages/std/src/math/uint256.rs index d7ef06cb38..4839ca7a24 100644 --- a/packages/std/src/math/uint256.rs +++ b/packages/std/src/math/uint256.rs @@ -222,7 +222,11 @@ impl From for String { impl fmt::Display for Uint256 { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.0.fmt(f) + // The inner type doesn't work as expected with padding, so we + // work around that. + let unpadded = self.0.to_string(); + + f.pad_integral(true, "", &unpadded) } } @@ -552,6 +556,12 @@ mod tests { assert_eq!(a.to_string(), "0"); } + #[test] + fn uint256_display_padding_works() { + let a = Uint256::from(123u64); + assert_eq!(format!("Embedded: {:05}", a), "Embedded: 00123"); + } + #[test] fn uint256_is_zero_works() { assert!(Uint256::zero().is_zero()); diff --git a/packages/std/src/math/uint512.rs b/packages/std/src/math/uint512.rs index 14f26e5192..00ef9d4169 100644 --- a/packages/std/src/math/uint512.rs +++ b/packages/std/src/math/uint512.rs @@ -253,7 +253,11 @@ impl From for String { impl fmt::Display for Uint512 { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.0.fmt(f) + // The inner type doesn't work as expected with padding, so we + // work around that. + let unpadded = self.0.to_string(); + + f.pad_integral(true, "", &unpadded) } } @@ -550,6 +554,12 @@ mod tests { assert_eq!(a.to_string(), "0"); } + #[test] + fn uint512_display_padding_works() { + let a = Uint512::from(123u64); + assert_eq!(format!("Embedded: {:05}", a), "Embedded: 00123"); + } + #[test] fn uint512_is_zero_works() { assert!(Uint512::zero().is_zero()); diff --git a/packages/std/src/math/uint64.rs b/packages/std/src/math/uint64.rs index b4020adfd2..c3274e481a 100644 --- a/packages/std/src/math/uint64.rs +++ b/packages/std/src/math/uint64.rs @@ -405,6 +405,12 @@ mod tests { assert_eq!(a.to_string(), "0"); } + #[test] + fn uint64_display_padding_works() { + let a = Uint64::from(123u64); + assert_eq!(format!("Embedded: {:05}", a), "Embedded: 00123"); + } + #[test] fn uint64_is_zero_works() { assert!(Uint64::zero().is_zero());