You can set the precision after invoking dc with the 'k' command, which pops a number off the stack and uses it to set the precision. But I always want a precision of three digits after the decimal by default. Is there a way to set a default precision in dc?
Is it possible to set a default precision for the Unix dc calculator? Is there a config file for it?
Asked
Active
Viewed 799 times
1 Answers
4
What I've done to achieve this is create a file in your home directory called .dcinit
with the commands you want executed every time, e.g.
bash-3.2$ cat .dcinit
5 k
bash-3.2$
Then define an alias in your startup config file for dc
that loads the startup file and then reads from stdin:
bash-3.2$ alias dc="dc -f ~/.dcinit -"
You should be able to use it as normal interactively but it will first load your .dcinit
:
bash-3.2$ dc
4 5 / p
.80000
If you need to run it on a file of dc
commands, you'll have to disable the alias or run it explicitly from /usr/bin/dc
or whereever.

cdlane
- 40,441
- 5
- 32
- 81
-
3You can just use `~/.dcrc` which it already executes automatically per the man page. – alexia Mar 30 '16 at 14:15
-
1@nyuszika7h, unfortunately, `.dcrc` isn't universal. It's not available on my system (OS X), not in the man page (`man -t dc | grep dcrc` returns nothing.) Nor will you find it in all `dc` man pages on the web, e.g. [FreeBSD General Commands Manual](https://www.freebsd.org/cgi/man.cgi?query=dc&sektion=1) which is why I had to come up with my work-around. – cdlane Mar 30 '16 at 19:18