diff --git a/io/src/lib.rs b/io/src/lib.rs index 82d51acc7b..e7878625e2 100644 --- a/io/src/lib.rs +++ b/io/src/lib.rs @@ -211,8 +211,7 @@ impl> Read for Cursor { let start_pos = self.pos.try_into().unwrap_or(inner.len()); let read = core::cmp::min(inner.len().saturating_sub(start_pos), buf.len()); buf[..read].copy_from_slice(&inner[start_pos..start_pos + read]); - self.pos = - self.pos.saturating_add(read.try_into().unwrap_or(u64::MAX /* unreachable */)); + self.pos = self.pos.saturating_add(read.try_into().unwrap_or(u64::MAX /* unreachable */)); Ok(read) } } diff --git a/units/src/amount.rs b/units/src/amount.rs index 94cfaca3e9..a66600a43c 100644 --- a/units/src/amount.rs +++ b/units/src/amount.rs @@ -817,13 +817,19 @@ fn fmt_satoshi_in( write!(f, "{}", num_before_decimal_point)?; repeat_char(f, '0', exp)?; - if total_decimals > 0 { write!(f, ".")?; } - if norm_nb_decimals > 0 { + if let Some(f_precision) = options.precision { + if f_precision < norm_nb_decimals && norm_nb_decimals > 0 { + write!(f, "{:0width$}", 0, width = f_precision)?; + } else if norm_nb_decimals > 0 { + write!(f, "{:0width$}", num_after_decimal_point, width = norm_nb_decimals)?; + } + } else if norm_nb_decimals > 0 { write!(f, "{:0width$}", num_after_decimal_point, width = norm_nb_decimals)?; } + repeat_char(f, '0', trailing_decimal_zeros)?; if show_denom { @@ -1476,8 +1482,15 @@ 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) + let satoshis = self.unsigned_abs().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, self.is_negative(), f, denomination, true, format_options) } } @@ -2415,10 +2428,10 @@ mod tests { btc_check_fmt_non_negative_6, 1, "{}", "0.00000001"; btc_check_fmt_non_negative_7, 1, "{:2}", "0.00000001"; btc_check_fmt_non_negative_8, 1, "{:02}", "0.00000001"; - btc_check_fmt_non_negative_9, 1, "{:.1}", "0.00000001"; + btc_check_fmt_non_negative_9, 1, "{:.1}", "0.0"; btc_check_fmt_non_negative_10, 1, "{:11}", " 0.00000001"; - btc_check_fmt_non_negative_11, 1, "{:11.1}", " 0.00000001"; - btc_check_fmt_non_negative_12, 1, "{:011.1}", "00.00000001"; + btc_check_fmt_non_negative_11, 1, "{:11.1}", " 0.0"; + btc_check_fmt_non_negative_12, 1, "{:011.1}", "00.0"; btc_check_fmt_non_negative_13, 1, "{:.9}", "0.000000010"; btc_check_fmt_non_negative_14, 1, "{:11.9}", "0.000000010"; btc_check_fmt_non_negative_15, 1, "{:011.9}", "0.000000010"; @@ -2433,7 +2446,7 @@ mod tests { btc_check_fmt_non_negative_24, 110_000_000, "{}", "1.1"; btc_check_fmt_non_negative_25, 100_000_001, "{}", "1.00000001"; btc_check_fmt_non_negative_26, 100_000_001, "{:1}", "1.00000001"; - btc_check_fmt_non_negative_27, 100_000_001, "{:.1}", "1.00000001"; + btc_check_fmt_non_negative_27, 100_000_001, "{:.1}", "1.0"; btc_check_fmt_non_negative_28, 100_000_001, "{:10}", "1.00000001"; btc_check_fmt_non_negative_29, 100_000_001, "{:11}", " 1.00000001"; btc_check_fmt_non_negative_30, 100_000_001, "{:011}", "01.00000001"; @@ -2465,7 +2478,7 @@ mod tests { check_format_non_negative_show_denom! { Bitcoin, " BTC"; - btc_check_fmt_non_negative_show_denom_0, 1, "{:14.1}", "0.00000001"; + btc_check_fmt_non_negative_show_denom_0, 1, "{:14.1}", "0.0"; btc_check_fmt_non_negative_show_denom_1, 1, "{:14.8}", "0.00000001"; btc_check_fmt_non_negative_show_denom_2, 1, "{:15}", " 0.00000001"; btc_check_fmt_non_negative_show_denom_3, 1, "{:015}", "00.00000001"; @@ -2940,18 +2953,52 @@ mod tests { 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!("{:.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"); - assert_eq!(format!("{:.2}", Amount::from_sat(10_000)), "0.0001 BTC"); - assert_eq!(format!("{:.2}", Amount::from_sat(100_000)), "0.001 BTC"); - assert_eq!(format!("{:.2}", Amount::from_sat(1_000_000)), "0.01 BTC"); - assert_eq!(format!("{:.2}", Amount::from_sat(10_000_000)), "0.10 BTC"); + assert_eq!(format!("{:.1}", Amount::from_sat(10)), "0.0 BTC"); + assert_eq!(format!("{:.2}", Amount::from_sat(10)), "0.00 BTC"); + assert_eq!(format!("{:.3}", Amount::from_sat(10)), "0.000 BTC"); + assert_eq!(format!("{:.4}", Amount::from_sat(10)), "0.0000 BTC"); + assert_eq!(format!("{:.5}", Amount::from_sat(10)), "0.00000 BTC"); + assert_eq!(format!("{:.6}", Amount::from_sat(10)), "0.000000 BTC"); + assert_eq!(format!("{:.7}", Amount::from_sat(10)), "0.0000001 BTC"); + assert_eq!(format!("{:.8}", Amount::from_sat(10)), "0.00000010 BTC"); + assert_eq!(format!("{:.9}", Amount::from_sat(10)), "0.000000100 BTC"); + assert_eq!(format!("{:.1}", Amount::from_sat(100_000_000)), "1.0 BTC"); assert_eq!(format!("{:.2}", Amount::from_sat(100_000_000)), "1.00 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!("{:.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_000)), "4000000 BTC"); + assert_eq!(format!("{}", Amount::from_sat(40_000_000_001)), "400.00000001 BTC"); + assert_eq!(format!("{}", Amount::from_sat(40_000_000_010)), "400.00000010 BTC"); + assert_eq!(format!("{:.7}", Amount::from_sat(40_000_000_010)), "400.0000001 BTC"); + assert_eq!(format!("{:.1}", Amount::from_sat(40_000_000_001)), "400.0 BTC"); + assert_eq!(format!("{}", Amount::from_btc(543.53524).unwrap()), "543.53524000 BTC"); + assert_eq!(format!("{:.5}", Amount::from_btc(543.53524).unwrap()), "543.53524 BTC"); + } + + #[test] + #[cfg(feature = "alloc")] + fn trailing_zeros_for_signed_amount() { + assert_eq!(format!("{}", -SignedAmount::ONE_SAT), "-0.00000001 BTC"); + assert_eq!(format!("{}", -SignedAmount::ONE_BTC), "-1 BTC"); + assert_eq!(format!("{}", SignedAmount::from_sat(-1)), "-0.00000001 BTC"); + assert_eq!(format!("{}", SignedAmount::from_sat(-10)), "-0.00000010 BTC"); + assert_eq!(format!("{:.1}", SignedAmount::from_sat(-10)), "-0.0 BTC"); + assert_eq!(format!("{:.2}", SignedAmount::from_sat(-10)), "-0.00 BTC"); + assert_eq!(format!("{:.3}", SignedAmount::from_sat(-10)), "-0.000 BTC"); + assert_eq!(format!("{:.4}", SignedAmount::from_sat(-10)), "-0.0000 BTC"); + assert_eq!(format!("{:.5}", SignedAmount::from_sat(-10)), "-0.00000 BTC"); + assert_eq!(format!("{:.6}", SignedAmount::from_sat(-10)), "-0.000000 BTC"); + assert_eq!(format!("{:.7}", SignedAmount::from_sat(-10)), "-0.0000001 BTC"); + assert_eq!(format!("{:.8}", SignedAmount::from_sat(-10)), "-0.00000010 BTC"); + assert_eq!(format!("{:.9}", SignedAmount::from_sat(-10)), "-0.000000100 BTC"); + assert_eq!(format!("{:.1}", SignedAmount::from_sat(-100_000_000)), "-1.0 BTC"); + assert_eq!(format!("{:.2}", SignedAmount::from_sat(-100_000_000)), "-1.00 BTC"); + assert_eq!(format!("{}", SignedAmount::from_sat(-100_000_000)), "-1 BTC"); + assert_eq!(format!("{}", SignedAmount::from_sat(-40_000_000_001)), "-400.00000001 BTC"); + assert_eq!(format!("{}", SignedAmount::from_sat(-40_000_000_010)), "-400.00000010 BTC"); + assert_eq!(format!("{:.7}", SignedAmount::from_sat(-40_000_000_010)), "-400.0000001 BTC"); + assert_eq!(format!("{:.1}", SignedAmount::from_sat(-40_000_000_001)), "-400.0 BTC"); + assert_eq!(format!("{}", SignedAmount::from_btc(543.53524).unwrap()), "543.53524000 BTC"); + assert_eq!(format!("{:.5}", SignedAmount::from_btc(543.53524).unwrap()), "543.53524 BTC"); + assert_eq!(format!("{}", SignedAmount::from_btc(-543.53524).unwrap()), "-543.53524000 BTC"); + assert_eq!(format!("{:.5}", SignedAmount::from_btc(-543.53524).unwrap()), "-543.53524 BTC"); } }