Skip to content

Commit d678e53

Browse files
drinkcatsylvestre
authored andcommitted
uucore: format: Fix uppercase hex floating point printing
Accidentally broke this use case when refactoring. Added a test as well.
1 parent e6c24b2 commit d678e53

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/uucore/src/lib/features/format/num_format.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,10 @@ fn format_float_hexadecimal(
585585
};
586586

587587
// Convert "XXX" to "X.XX": that divides by 16^precision = 2^(4*precision), so add that to the exponent.
588-
let digits = frac2.to_str_radix(16);
588+
let mut digits = frac2.to_str_radix(16);
589+
if case == Case::Uppercase {
590+
digits.make_ascii_uppercase();
591+
}
589592
let (first_digit, remaining_digits) = digits.split_at(1);
590593
let exponent = exp2 + (4 * precision) as i64;
591594

@@ -914,6 +917,17 @@ mod test {
914917
assert_eq!(f("0"), "0x0.p+0");
915918
assert_eq!(f("0.125"), "0x8.p-6");
916919
assert_eq!(f("256.0"), "0x8.p+5");
920+
921+
let f = |x| {
922+
format_float_hexadecimal(
923+
&BigDecimal::from_str(x).unwrap(),
924+
6,
925+
Case::Uppercase,
926+
ForceDecimal::No,
927+
)
928+
};
929+
assert_eq!(f("0.00001"), "0xA.7C5AC4P-20");
930+
assert_eq!(f("0.125"), "0x8.000000P-6");
917931
}
918932

919933
#[test]

0 commit comments

Comments
 (0)