0

My script

#!/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 "

if input for number 1 is -1 , and number 2 is 0.9, why the result only -.1.

I want to show the zero like this.

Your result : -1 + 0.9 = -0.1

How I can do it?

4

1 回答 1

1

因为您现在只考虑 case .NNN,而不是-.NNN, 之前有减号-

有了这个它应该工作:

sed    -e 's/^\./0./'       -e 's/^-\./-0./'
           start with .         start with -.

全部一起;

jlh=$(echo $number1 + $number2 | bc -l | sed -e 's/^\./0./' -e 's/^-\./-0./');
于 2013-05-10T12:34:45.023 回答