make into string --decimals add decimals to integer numbers (#6084)

* make `into string --decimals` add decimals to integer numbers

* add exception for 0
This commit is contained in:
pwygab 2022-07-20 19:16:35 +08:00 committed by GitHub
parent 410f3ef0f0
commit 558cd58d09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -53,6 +53,14 @@ impl Command for SubCommand {
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![ vec![
Example {
description: "convert integer to string and append three decimal places",
example: "5 | into string -d 3",
result: Some(Value::String {
val: "5.000".to_string(),
span: Span::test_data(),
}),
},
Example { Example {
description: "convert decimal to string and round to nearest integer", description: "convert decimal to string and round to nearest integer",
example: "1.7 | into string -d 0", example: "1.7 | into string -d 0",
@ -210,6 +218,15 @@ pub fn action(
Value::Int { val, .. } => { Value::Int { val, .. } => {
let res = if group_digits { let res = if group_digits {
format_int(*val) // int.to_formatted_string(*locale) format_int(*val) // int.to_formatted_string(*locale)
} else if let Some(dig) = digits {
let mut val_with_trailing_zeroes = val.to_string();
if dig != 0 {
val_with_trailing_zeroes.push('.');
}
for _ in 0..dig {
val_with_trailing_zeroes.push('0');
}
val_with_trailing_zeroes
} else { } else {
val.to_string() val.to_string()
}; };