1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 03:57:44 +00:00

Merge pull request #4089 from cakebaker/numfmt_rounding

numfmt: round values if precision is 0
This commit is contained in:
Sylvestre Ledru 2022-10-27 19:55:15 +02:00 committed by GitHub
commit be5426bba7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 3 deletions

View file

@ -272,14 +272,13 @@ fn transform_to(
let (i2, s) = consider_suffix(s, &opts.to, round_method, precision)?;
let i2 = i2 / (opts.to_unit as f64);
Ok(match s {
None if precision > 0 => {
None => {
format!(
"{:.precision$}",
round_with_precision(i2, round_method, precision),
precision = precision
)
}
None => format!("{}", i2),
Some(s) if precision > 0 => {
format!(
"{:.precision$}{}",

View file

@ -530,7 +530,7 @@ fn test_round() {
new_ucmd!()
.args(&[
"--to=si",
&format!("--round={}", method),
&format!("--round={method}"),
"--",
"9001",
"-9001",
@ -542,6 +542,32 @@ fn test_round() {
}
}
#[test]
fn test_round_with_to_unit() {
for (method, exp) in [
("from-zero", ["6", "-6", "5.9", "-5.9", "5.86", "-5.86"]),
("towards-zero", ["5", "-5", "5.8", "-5.8", "5.85", "-5.85"]),
("up", ["6", "-5", "5.9", "-5.8", "5.86", "-5.85"]),
("down", ["5", "-6", "5.8", "-5.9", "5.85", "-5.86"]),
("nearest", ["6", "-6", "5.9", "-5.9", "5.86", "-5.86"]),
] {
new_ucmd!()
.args(&[
"--to-unit=1024",
&format!("--round={method}"),
"--",
"6000",
"-6000",
"6000.0",
"-6000.0",
"6000.00",
"-6000.00",
])
.succeeds()
.stdout_only(exp.join("\n") + "\n");
}
}
#[test]
fn test_suffix_is_added_if_not_supplied() {
new_ucmd!()