2

When I run:

fval=1.40 ; echo "scale=0 ; 1000 * ${fval}" | bc

I get the result:

1400.00

With my specified value for scale, I expect no decimal digits.

Does anyone else have this issue?

Version: bc 1.07.1

Ubuntu 22.04.4 LTS

(Upgrade of Ubuntu MATE from 20.04 LTS to 22.04.04 LTS, not a fresh install)

kos
  • 41,268

1 Answers1

9

Not a bug. From man bc:

Unless specifically mentioned the scale of the result is the maximum scale of the expressions involved.

And, further down below:

expr * expr
The result of the expression is the product of the two expressions.

expr / expr
The result of the expression is the quotient of the two expressions. The scale of the result is the value of the variable scale.

So, the fact that "The scale of the result is the value of the variable scale" is not explicitly stated (unlike when describing expr / expr), already means that expr * expr is not expected to honor scale.

Also, the fact that in case of expr * expr "the result is the maximum scale of the expressions involved" can be confirmed empirically:

% fval=1.40 ; echo "scale=0 ; 1000 * ${fval}" | bc
1400.00 # two decimal digits as in fval
% fval=1.400 ; echo "scale=0 ; 1000 * ${fval}" | bc
1400.000 # three decimal digits as in fval
% fval=1.400 ; echo "scale=0 ; 1000.0000 * ${fval}" | bc
1400.0000 # four decimal digits as in 1000.0000

To obtain the wanted behavior, you could add a meaningless division (note that since scale defaults to 0 you might as well omit it):

fval=1.40 ; echo "1000 * ${fval} / 1" | bc
% fval=1.40 ; echo "1000 * ${fval} / 1" | bc
1400
kos
  • 41,268