mirror of
https://github.com/nushell/nushell
synced 2024-11-10 23:24:14 +00:00
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:
parent
410f3ef0f0
commit
558cd58d09
1 changed files with 17 additions and 0 deletions
|
@ -53,6 +53,14 @@ impl Command for SubCommand {
|
|||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
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 {
|
||||
description: "convert decimal to string and round to nearest integer",
|
||||
example: "1.7 | into string -d 0",
|
||||
|
@ -210,6 +218,15 @@ pub fn action(
|
|||
Value::Int { val, .. } => {
|
||||
let res = if group_digits {
|
||||
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 {
|
||||
val.to_string()
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue