Skip to content

Commit dba5491

Browse files
authored
numfmt: fix zero-padding placement for negative numbers (#11694)
* numfmt: fix zero-padding placement before sign for negative numbers When using zero-padded format (e.g. %018.2f), the sign character is now placed before the padding zeros, matching C printf behavior. Fixes #11664 * numfmt: simplify sign handling with combined strip_prefix
1 parent 32cf4cd commit dba5491

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/uu/numfmt/src/format.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,12 @@ fn format_string(
581581
let padded_number = match padding {
582582
0 => number_with_suffix,
583583
p if p > 0 && options.format.zero_padding => {
584-
let zero_padded = pad_string(&number_with_suffix, p as usize, '0', true);
584+
let zero_padded = if let Some(unsigned) = number_with_suffix.strip_prefix(['-', '+']) {
585+
let sign = &number_with_suffix[..1];
586+
format!("{sign}{}", pad_string(unsigned, p as usize - 1, '0', true))
587+
} else {
588+
pad_string(&number_with_suffix, p as usize, '0', true)
589+
};
585590

586591
match implicit_padding.unwrap_or(options.padding) {
587592
0 => zero_padded,

tests/by-util/test_numfmt.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1428,7 +1428,6 @@ fn test_from_unit_fractional_precision_issue_11663() {
14281428
// Zero-padded `--format` places padding zeros before the sign for negative
14291429
// numbers; GNU (and C printf) puts the sign first.
14301430
#[test]
1431-
#[ignore = "GNU compat: see uutils/coreutils#11664"]
14321431
fn test_zero_pad_sign_order_issue_11664() {
14331432
new_ucmd!()
14341433
.args(&["--from=none", "--format=%018.2f", "--", "-9869647"])

0 commit comments

Comments
 (0)