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 d887423efc
adds a test
to prevent this change and also fixes the problem with `{:0.8}` not
working.
Closes #2136
Closes #2948
This commit is contained in:
parent
d36141b5a7
commit
7c95a777c1
|
@ -1070,15 +1070,7 @@ impl fmt::Debug for Amount {
|
||||||
// Just using Bitcoin denominated string.
|
// Just using Bitcoin denominated string.
|
||||||
impl fmt::Display for Amount {
|
impl fmt::Display for Amount {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
let satoshis = self.to_sat();
|
fmt::Display::fmt(&self.display_in(Denomination::Bitcoin).show_denomination(), f)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1478,8 +1470,7 @@ impl fmt::Debug for SignedAmount {
|
||||||
// Just using Bitcoin denominated string.
|
// Just using Bitcoin denominated string.
|
||||||
impl fmt::Display for SignedAmount {
|
impl fmt::Display for SignedAmount {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
self.fmt_value_in(f, Denomination::Bitcoin)?;
|
fmt::Display::fmt(&self.display_in(Denomination::Bitcoin).show_denomination(), f)
|
||||||
write!(f, " {}", Denomination::Bitcoin)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2876,10 +2867,11 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
fn trailing_zeros_for_amount() {
|
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_SAT), "0.00000001 BTC");
|
||||||
assert_eq!(format!("{}", Amount::ONE_BTC), "1 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(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(10)), "0.0000001 BTC");
|
||||||
assert_eq!(format!("{:.2}", Amount::from_sat(100)), "0.000001 BTC");
|
assert_eq!(format!("{:.2}", Amount::from_sat(100)), "0.000001 BTC");
|
||||||
assert_eq!(format!("{:.2}", Amount::from_sat(1000)), "0.00001 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(100_000_000)), "1 BTC");
|
||||||
assert_eq!(format!("{}", Amount::from_sat(40_000_000_000)), "400 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!("{:.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");
|
assert_eq!(format!("{}", Amount::from_sat(400_000_000_000_000)), "4000000 BTC");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue