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?
this only uses bc, and works with negative numbers:
try it with:
I cannot find anything about output format in the documentation. Instead of sed, you can also reach for printf:
For positive numbers, it may be as simple as printing (an string) zero:
avoid the zero if the number is bigger (or equal) to 1:
It gets a bit more complex if the number may be negative:
You may define a function and add it to a library:
This one will also handle negative numbers:
This might work for you:
You can also resort to awk to format:
or with awk itself doing the math: