You can't do this with the obvious scale=0
because of the way that the scale is determined.
The documentation indirectly explains that dividing by one is sufficient to reset the output to match the value of scale
, which defaults to zero:
expr1 / expr2 The result of the expression is the quotient of the two expressions. The scale of the result is the value of the variable scale.
p=12.34; echo "($p*100)" | bc
1234.00
p=12.34; echo "($p*100)/1" | bc
1234
If your version of bc
does not handle this, pipe it through sed
instead:
p=12.34; echo "($p*100)" | bc | sed -E -e 's!(\.[0-9]*[1-9])0*$!\1!' -e 's!(\.0*)$!!'
1234
This pair of REs will strip trailing zeros from the decimal part of a number. So 3.00 will reduce to 3, and 3.10 will reduce to 3.1, but 300 will remain unchanged.
Alternatively, use perl
and dispense with bc
in the first place:
p=12.34; perl -e '$p = shift; print $p * 100, "\n"' "$p"
scale=2;
– Ipor Sircer Nov 03 '16 at 00:15