From 558cd58d096c71ff7be988f3e351bb077353652f Mon Sep 17 00:00:00 2001 From: pwygab <88221256+merelymyself@users.noreply.github.com> Date: Wed, 20 Jul 2022 19:16:35 +0800 Subject: [PATCH] make `into string --decimals` add decimals to integer numbers (#6084) * make `into string --decimals` add decimals to integer numbers * add exception for 0 --- .../nu-command/src/conversions/into/string.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/crates/nu-command/src/conversions/into/string.rs b/crates/nu-command/src/conversions/into/string.rs index 7e8093717c..ad6da56514 100644 --- a/crates/nu-command/src/conversions/into/string.rs +++ b/crates/nu-command/src/conversions/into/string.rs @@ -53,6 +53,14 @@ impl Command for SubCommand { fn examples(&self) -> 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 { 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() };