Krzysztof Lewko
Krzysztof Lewko

Reputation: 1002

Bash and minus floating points

I have code like this in Bash :

read a
read b
c=96.0
d=100.0
echo "scale=2;($b*$c - $a*$d)/$a" |bc 

And it prints result of this expression :

(b*96-a*100)/a

But when result is between -1 and 0 it gives something like this : -.99

For smaller values it prints result correctly. So, my question is, how to force program to put 0 when printing

0.123123(...)

? Not only

.123123(...)

Upvotes: 0

Views: 354

Answers (1)

sehe
sehe

Reputation: 393457

Use printf:

$ printf "%0.2f" "$(echo 'scale=2; 1.9/10.0' | bc)"
0.19

printf is (also) a bash builtin

Upvotes: 2

Related Questions