Ubuntu 14.04.1 LTS
How do I set the default scale for the bc calculator? Each time I run bc I want scale=2 to be the default, I want to limit all calculations to 2 decimal places. I made a file in my home dir called .bc
and inside it I put scale=2
on the first line followed by a carriage return.
Permissions on ~/.bc are: -rw-rw-rw-
. Is that right?
Then I did set BC_ENV_ARGS=~/.bc; export BC_ENV_ARGS
. Then I ran bc, did a test like 8.37843*32.190233, and still got more than 2 decimal places.
The online manual did not provide any examples on doing this, so please don't direct me to there.
Thanks.
EDIT: Ok when I do a test like 78/31 it gives me 2 decimals. But when I do my test above, it gives me more than 2 decimals. Why is that? I always want to show only 2 decimals.
As muru said, the scale of the result is the maximum scale of the expression involved. but if you want to set the scale for division (want to set the truncation level), place a file
.bc in your home (ex. /home/yourid/.bc)
and edit it to contain (the file name can be anything)Then in your .cshrc file put
Thisway, your default scale is set to 8.
From
man bc
(emphasis mine):Reading further, it seems
scale
is mostly only applicable if division is involved (/
,^
with negative exponents,%
, etc.).Therefore either use some other tool to get it printed the way you want (like
printf
orawk
), or divide by 1:quick addendum to what muru said for bash users:
Instead of cshrc, add the following line to your
.bashrc
in/home/yourname/.bashrc
:Put this in your
~/.bashrc
, it will give you two decimals:Do a
. ~/.bashrc
afterwards to load the changes.Invoke
bc
with the-l
option to have scale set to 20 automatically.