在bash脚本中将前导零添加到浮点数
问题描述:
我的脚本
#!/bin/bash
echo -n "number 1 :";
read number1
echo -n "number 2 :";
read number2
jlh=$(echo $number1 + $number2 | bc -l | sed 's/^\./0./');
echo "your result : $number1 + $number2 = $jlh "
如果数字1的输入为 -1
,数字2的输入为 0.9
,为什么结果仅为-.1
.
if input for number 1 is -1
, and number 2 is 0.9
, why the result only -.1
.
我想这样显示零.
Your result : -1 + 0.9 = -0.1
我该怎么做?
答
因为您现在只考虑情况 .NNN
,而不是-.NNN
,在前面加上减号-
:
Because you by now just consider the case .NNN
, but not the -.NNN
, that is having the minus -
sign before:
通过它应该可以工作:
sed -e 's/^\./0./' -e 's/^-\./-0./'
start with . start with -.
在一起;
jlh=$(echo $number1 + $number2 | bc -l | sed -e 's/^\./0./' -e 's/^-\./-0./');