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
This commit is contained in:
Sim-hu
2026-04-07 13:26:40 +02:00
committed by GitHub
parent 32cf4cdf0c
commit dba549141c
2 changed files with 6 additions and 2 deletions
+6 -1
View File
@@ -581,7 +581,12 @@ fn format_string(
let padded_number = match padding {
0 => number_with_suffix,
p if p > 0 && options.format.zero_padding => {
let zero_padded = pad_string(&number_with_suffix, p as usize, '0', true);
let zero_padded = if let Some(unsigned) = number_with_suffix.strip_prefix(['-', '+']) {
let sign = &number_with_suffix[..1];
format!("{sign}{}", pad_string(unsigned, p as usize - 1, '0', true))
} else {
pad_string(&number_with_suffix, p as usize, '0', true)
};
match implicit_padding.unwrap_or(options.padding) {
0 => zero_padded,
-1
View File
@@ -1428,7 +1428,6 @@ fn test_from_unit_fractional_precision_issue_11663() {
// Zero-padded `--format` places padding zeros before the sign for negative
// numbers; GNU (and C printf) puts the sign first.
#[test]
#[ignore = "GNU compat: see uutils/coreutils#11664"]
fn test_zero_pad_sign_order_issue_11664() {
new_ucmd!()
.args(&["--from=none", "--format=%018.2f", "--", "-9869647"])