how to make bc to show me 10 and not 10.00

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 's!\.0*$!!'
1234

This RE will only strip trailing zeros from an integer number. So 3.00 will reduce to 3, but 3.10 will not reduce to 3.1. If you really need the full ability to strip the trailing zeros from a decimal number, a PCRE is required:

p=12.34; echo "($p*100)" | bc | perl -pe '/\./ && s/0+$/$1/ && s/\.$//'

But if you're going to use perl then you might as well dispense with bc in the first place:

p=12.34; perl -e '$p = shift; print $p * 100, "\n"' "$p"

you can use awk to calculate the values

bash-3.2$ p=0.01
bash-3.2$ q=$(awk -vp_val="$p" 'BEGIN{print p_val*100}')
bash-3.2$ echo $q
1


bash-3.2$ p=0.02
bash-3.2$ q=$(awk -vp_val="$p" 'BEGIN{print p_val*100}')
bash-3.2$ echo $q
2


bash-3.2$ p=0.022
bash-3.2$ q=$(awk -vp_val="$p" 'BEGIN{print p_val*100}')
bash-3.2$ echo $q
2.2

TL;DR

You have lots of options. bc has known behavior where scale=0 doesn't always do what you expect, but there are a lot of workarounds. Here are just a few.

printf

Use printf to limit your output to integers.

$ printf "%g\n" $(echo '12.34 * 100' | bc)
1234

bc with division

If you want to stick with bc scaling, you need to specify both a scale of zero and divide by 1 to reset the scale. This is known behavior, but I really can't explain the why of it.

$ echo '12.34 * 100 / 1' | scale=0 bc
1234

sed

Just strip off the unwanted trailing characters.

$ echo '12.34 * 100' | bc | sed 's/\.00$//'
1234

bash

Use a brace expansion to return the value before the decimal.

$ p='12.34'; q=$(bc <<< "scale=2; $p*100"); echo ${q%%.00}
1234

Tags:

Bc