echo "scale=3;1/8" | bc
shows .125
on the screen. How to show 0.125
if the output result is less than one?
echo "scale=3;1/8" | bc
shows .125
on the screen. How to show 0.125
if the output result is less than one?
You can pipe into awk
echo "scale=3;1/8" | bc | awk '{printf "%.3f\n", $0}'
or you could just use awk for it all
awk '{printf "%.3f\n", 1/8}' <<< ""
0.125
here string
. So basically pretends there is an empty file at the end, otherwise awk will read from stdin.More info here
–
Apr 22 '15 at 14:49
BEGIN
block prevent you from that trouble and it's portable.
– cuonglm
Apr 22 '15 at 14:59
Bettering @cuonglm's answer:
a=10.543; b=`printf '%.6f' "$(echo "$a/100" | bc -l)"`; echo $b;
Use "bc -l" to use math library.