From d678e5320f19b93c1ab345a02c58cf687c4b05a4 Mon Sep 17 00:00:00 2001 From: Nicolas Boichat Date: Thu, 20 Mar 2025 20:43:09 +0100 Subject: [PATCH] uucore: format: Fix uppercase hex floating point printing Accidentally broke this use case when refactoring. Added a test as well. --- src/uucore/src/lib/features/format/num_format.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/uucore/src/lib/features/format/num_format.rs b/src/uucore/src/lib/features/format/num_format.rs index 814c18dbd..e157fa5ec 100644 --- a/src/uucore/src/lib/features/format/num_format.rs +++ b/src/uucore/src/lib/features/format/num_format.rs @@ -585,7 +585,10 @@ fn format_float_hexadecimal( }; // Convert "XXX" to "X.XX": that divides by 16^precision = 2^(4*precision), so add that to the exponent. - let digits = frac2.to_str_radix(16); + let mut digits = frac2.to_str_radix(16); + if case == Case::Uppercase { + digits.make_ascii_uppercase(); + } let (first_digit, remaining_digits) = digits.split_at(1); let exponent = exp2 + (4 * precision) as i64; @@ -914,6 +917,17 @@ mod test { assert_eq!(f("0"), "0x0.p+0"); assert_eq!(f("0.125"), "0x8.p-6"); assert_eq!(f("256.0"), "0x8.p+5"); + + let f = |x| { + format_float_hexadecimal( + &BigDecimal::from_str(x).unwrap(), + 6, + Case::Uppercase, + ForceDecimal::No, + ) + }; + assert_eq!(f("0.00001"), "0xA.7C5AC4P-20"); + assert_eq!(f("0.125"), "0x8.000000P-6"); } #[test]