Force floats to output a decimal in nuon (#5768)

* Force floats to output a decimal in nuon

* Add test
This commit is contained in:
JT 2022-06-14 05:45:07 +12:00 committed by GitHub
parent 9dbf7556b8
commit 7ae7394c85
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 1 deletions

View file

@ -86,7 +86,13 @@ fn value_to_string(v: &Value, span: Span) -> Result<String, ShellError> {
span, span,
)), )),
Value::Filesize { val, .. } => Ok(format!("{}b", *val)), Value::Filesize { val, .. } => Ok(format!("{}b", *val)),
Value::Float { val, .. } => Ok(format!("{}", *val)), Value::Float { val, .. } => {
if &val.round() == val {
Ok(format!("{}.0", *val))
} else {
Ok(format!("{}", *val))
}
}
Value::Int { val, .. } => Ok(format!("{}", *val)), Value::Int { val, .. } => Ok(format!("{}", *val)),
Value::List { vals, .. } => { Value::List { vals, .. } => {
let headers = get_columns(vals); let headers = get_columns(vals);

View file

@ -190,3 +190,15 @@ fn read_bool() {
assert_eq!(actual.out, "true") assert_eq!(actual.out, "true")
} }
#[test]
fn float_doesnt_become_int() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
1.0 | to nuon
"#
));
assert_eq!(actual.out, "1.0")
}