From 7c95a777c1435eb1aa53705fb97ed02411853614 Mon Sep 17 00:00:00 2001 From: Martin Habovstiak Date: Tue, 2 Jul 2024 09:11:33 +0200 Subject: [PATCH] Fix `Amount` decimals handling Displaying with minimal number of zeros is the default in Rust and we want to follow it. This was originally implemented in #716 but #2604 reversed it claiming this is common, however it broke people who rely on minimal display (e.g. BIP21) without fixing the root cause of #2136. This reverts commit d887423efcdad8db4c60ffe61f53d2356c82c87c adds a test to prevent this change and also fixes the problem with `{:0.8}` not working. Closes #2136 Closes #2948 --- units/src/amount.rs | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/units/src/amount.rs b/units/src/amount.rs index 2b9cf97b8..1f8b7d923 100644 --- a/units/src/amount.rs +++ b/units/src/amount.rs @@ -1070,15 +1070,7 @@ impl fmt::Debug for Amount { // Just using Bitcoin denominated string. impl fmt::Display for Amount { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let satoshis = self.to_sat(); - let denomination = Denomination::Bitcoin; - let mut format_options = FormatOptions::from_formatter(f); - - if f.precision().is_none() && satoshis.rem_euclid(Amount::ONE_BTC.to_sat()) != 0 { - format_options.precision = Some(8); - } - - fmt_satoshi_in(satoshis, false, f, denomination, true, format_options) + fmt::Display::fmt(&self.display_in(Denomination::Bitcoin).show_denomination(), f) } } @@ -1478,8 +1470,7 @@ impl fmt::Debug for SignedAmount { // Just using Bitcoin denominated string. impl fmt::Display for SignedAmount { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.fmt_value_in(f, Denomination::Bitcoin)?; - write!(f, " {}", Denomination::Bitcoin) + fmt::Display::fmt(&self.display_in(Denomination::Bitcoin).show_denomination(), f) } } @@ -2876,10 +2867,11 @@ mod tests { #[test] #[cfg(feature = "alloc")] fn trailing_zeros_for_amount() { + assert_eq!(format!("{}", Amount::from_sat(1000000)), "0.01 BTC"); assert_eq!(format!("{}", Amount::ONE_SAT), "0.00000001 BTC"); assert_eq!(format!("{}", Amount::ONE_BTC), "1 BTC"); assert_eq!(format!("{}", Amount::from_sat(1)), "0.00000001 BTC"); - assert_eq!(format!("{}", Amount::from_sat(10)), "0.00000010 BTC"); + assert_eq!(format!("{}", Amount::from_sat(10)), "0.0000001 BTC"); assert_eq!(format!("{:.2}", Amount::from_sat(10)), "0.0000001 BTC"); assert_eq!(format!("{:.2}", Amount::from_sat(100)), "0.000001 BTC"); assert_eq!(format!("{:.2}", Amount::from_sat(1000)), "0.00001 BTC"); @@ -2891,7 +2883,7 @@ mod tests { assert_eq!(format!("{}", Amount::from_sat(100_000_000)), "1 BTC"); assert_eq!(format!("{}", Amount::from_sat(40_000_000_000)), "400 BTC"); assert_eq!(format!("{:.10}", Amount::from_sat(100_000_000)), "1.0000000000 BTC"); - assert_eq!(format!("{}", Amount::from_sat(400_000_000_000_010)), "4000000.00000010 BTC"); + assert_eq!(format!("{}", Amount::from_sat(400_000_000_000_010)), "4000000.0000001 BTC"); assert_eq!(format!("{}", Amount::from_sat(400_000_000_000_000)), "4000000 BTC"); } }