Skip to content

numfmt: fix zero-padding placement for negative numbers#11694

Merged
cakebaker merged 2 commits intouutils:mainfrom
Sim-hu:fix/numfmt-sign-padding
Apr 7, 2026
Merged

numfmt: fix zero-padding placement for negative numbers#11694
cakebaker merged 2 commits intouutils:mainfrom
Sim-hu:fix/numfmt-sign-padding

Conversation

@Sim-hu
Copy link
Copy Markdown
Contributor

@Sim-hu Sim-hu commented Apr 7, 2026

Closes #11664

When using --format '%018.2f' with negative numbers, zero-padding was placed before the sign:

$ echo '-9869647' | numfmt --format '%018.2f'
0000000-9869647.00   # before
-00000009869647.00   # after (matches GNU/C printf)

The fix strips the sign character before zero-padding, pads the unsigned portion (with width reduced by 1), then prepends the sign back. Handles both - and +.

Enabled the existing ignored test for this issue.

When using zero-padded format (e.g. %018.2f), the sign character is now
placed before the padding zeros, matching C printf behavior.

Fixes uutils#11664
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 7, 2026

GNU testsuite comparison:

Skip an intermittent issue tests/date/resolution (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/date/date-locale-hour (passes in this run but fails in the 'main' branch)
Note: The gnu test tests/cp/link-heap is now being skipped but was previously passing.
Note: The gnu test tests/seq/seq-epipe is now being skipped but was previously passing.
Congrats! The gnu test tests/printf/printf-surprise is now passing!

Comment thread src/uu/numfmt/src/format.rs Outdated
Comment on lines +584 to +590
let zero_padded = if let Some(unsigned) = number_with_suffix.strip_prefix('-') {
format!("-{}", pad_string(unsigned, p as usize - 1, '0', true))
} else if let Some(unsigned) = number_with_suffix.strip_prefix('+') {
format!("+{}", pad_string(unsigned, p as usize - 1, '0', true))
} else {
pad_string(&number_with_suffix, p as usize, '0', true)
};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this works fine, you can simplify it by passing both prefixes (['-', '+']) to strip_prefix.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, updated to use a combined strip_prefix(['-', '+']). Also removed the unnecessary borrow per clippy.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 7, 2026

GNU testsuite comparison:

Skip an intermittent issue tests/date/date-locale-hour (fails in this run but passes in the 'main' branch)
Note: The gnu test tests/env/env-signal-handler was skipped on 'main' but is now failing.

@cakebaker cakebaker merged commit dba5491 into uutils:main Apr 7, 2026
169 checks passed
@cakebaker
Copy link
Copy Markdown
Contributor

Thanks for your PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

numfmt: zero-padded --format places padding before the sign for negative numbers

2 participants