bc is ignoring scale option

前端 未结 2 1285
别那么骄傲
别那么骄傲 2021-02-05 03:34

I can\'t figure out why bc tool sometimes ignores the scale option.

Here is an example:

> echo \'scale=2; 2.777 - 1.4744\' | bc
1.3026
相关标签:
2条回答
  • 2021-02-05 04:15

    From the bc(1) man page:

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

    1.4744 has scale 4, so that's what happens to your expression.

    0 讨论(0)
  • 2021-02-05 04:27

    as Carl pointed out, if you check man page, you can find that line. it is about expression explanations. subtraction won't read scale variable. If you want to get the expected result (1.30), you could:

    kent$  echo 'scale=2; (2.777 - 1.4744)/1' | bc 
    1.30
    

    / operation will read scale variable.

    0 讨论(0)
提交回复
热议问题