Search code examples
bashunixbc

How do I get bc(1) to print the leading zero?


I do something like the following in a Makefile:

echo "0.1 + 0.1" | bc

(in the real file the numbers are dynamic, of course)

It prints .2 but I want it to print 0.2.

I would like to do this without resorting to sed but I can't seem to find how to get bc to print the zero. Or is bc just not able to do this?


Solution

  • You can also resort to awk to format:

     echo "0.1 + 0.1" | bc | awk '{printf "%f", $0}'
    

    or with awk itself doing the math:

     echo "0.1 0.1" | awk '{printf "%f", $1 + $2}'