mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
numfmt: round values if precision is 0
This commit is contained in:
parent
3cb1b9466a
commit
2eb0b6dfe0
2 changed files with 28 additions and 3 deletions
|
@ -272,14 +272,13 @@ fn transform_to(
|
||||||
let (i2, s) = consider_suffix(s, &opts.to, round_method, precision)?;
|
let (i2, s) = consider_suffix(s, &opts.to, round_method, precision)?;
|
||||||
let i2 = i2 / (opts.to_unit as f64);
|
let i2 = i2 / (opts.to_unit as f64);
|
||||||
Ok(match s {
|
Ok(match s {
|
||||||
None if precision > 0 => {
|
None => {
|
||||||
format!(
|
format!(
|
||||||
"{:.precision$}",
|
"{:.precision$}",
|
||||||
round_with_precision(i2, round_method, precision),
|
round_with_precision(i2, round_method, precision),
|
||||||
precision = precision
|
precision = precision
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
None => format!("{}", i2),
|
|
||||||
Some(s) if precision > 0 => {
|
Some(s) if precision > 0 => {
|
||||||
format!(
|
format!(
|
||||||
"{:.precision$}{}",
|
"{:.precision$}{}",
|
||||||
|
|
|
@ -530,7 +530,7 @@ fn test_round() {
|
||||||
new_ucmd!()
|
new_ucmd!()
|
||||||
.args(&[
|
.args(&[
|
||||||
"--to=si",
|
"--to=si",
|
||||||
&format!("--round={}", method),
|
&format!("--round={method}"),
|
||||||
"--",
|
"--",
|
||||||
"9001",
|
"9001",
|
||||||
"-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]
|
#[test]
|
||||||
fn test_suffix_is_added_if_not_supplied() {
|
fn test_suffix_is_added_if_not_supplied() {
|
||||||
new_ucmd!()
|
new_ucmd!()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue