Sunday, June 8, 2008

Bash float computations - bc awk


As "expr" does not help in float computations, here are some of the alternatives

___________________________
float computations using bc
___________________________


$ Number=`echo 80 \* 10.69 | bc`; echo $Number

855.20


_____________________________
float computations using awk
_____________________________


$ Number=`(echo - | awk '{ print 80*10.69}')`; echo $Number

855.2

And if you are supplying something from outside

$ Number=`(echo - | awk '{ print 80*10.69}')`; echo $Number

855.2

One more way of passing variables to AWK

$ S=80; Number=`echo - | awk -v K=$S '{ print K*10.69}'` ; echo $Number

855.2

No comments:

© Jadu Saikia www.UNIXCL.com