mirror of
https://github.com/uutils/coreutils.git
synced 2026-06-10 15:48:22 -07:00
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:
@@ -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,
|
||||
|
||||
@@ -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"])
|
||||
|
||||
Reference in New Issue
Block a user