mirror of
https://github.com/uutils/coreutils.git
synced 2026-06-10 15:48:22 -07:00
numfmt: preserve fractional digits when --from-unit is used without suffix and fixed String format (#11674)
* numfmt: preserve fractional digits when --from-unit is used without suffix and fixed String format * numfmt: simplify redundant if branches caught by clippy * numfmt: fix precision condition to only preserve decimals when input has no unit suffix
This commit is contained in:
@@ -374,11 +374,12 @@ fn transform_from(s: &str, opts: &TransformOptions, options: &NumfmtOptions) ->
|
||||
.map_err(|original| {
|
||||
detailed_error_message(s, opts.from, &options.unit_separator).unwrap_or(original)
|
||||
})?;
|
||||
let had_no_suffix = suffix.is_none();
|
||||
let i = i * (opts.from_unit as f64);
|
||||
|
||||
remove_suffix(i, suffix, opts.from).map(|n| {
|
||||
// GNU numfmt doesn't round values if no --from argument is provided by the user
|
||||
if opts.from == Unit::None {
|
||||
if opts.from == Unit::None || had_no_suffix {
|
||||
if n == -0.0 { 0.0 } else { n }
|
||||
} else if n < 0.0 {
|
||||
-n.abs().ceil()
|
||||
@@ -541,7 +542,12 @@ fn format_string(
|
||||
|
||||
let precision = if let Some(p) = options.format.precision {
|
||||
p
|
||||
} else if options.transform.from == Unit::None && options.transform.to == Unit::None {
|
||||
} else if options.transform.to == Unit::None
|
||||
&& !source_without_suffix
|
||||
.chars()
|
||||
.last()
|
||||
.is_some_and(char::is_alphabetic)
|
||||
{
|
||||
parse_implicit_precision(source_without_suffix)
|
||||
} else {
|
||||
0
|
||||
|
||||
@@ -1417,7 +1417,6 @@ fn test_to_auto_rejected_at_parse_time_issue_11662() {
|
||||
// `--from-unit` multiplication with fractional input rounds to an integer;
|
||||
// GNU preserves the fractional digits.
|
||||
#[test]
|
||||
#[ignore = "GNU compat: see uutils/coreutils#11663"]
|
||||
fn test_from_unit_fractional_precision_issue_11663() {
|
||||
new_ucmd!()
|
||||
.args(&["--from=iec", "--from-unit=959", "--", "-615484.454"])
|
||||
|
||||
Reference in New Issue
Block a user