Disjointed ideas, text dumps, archives, and thoughts posted infrequently.

Archive Post: Setting the scale in dc

A brief but fun post from my ancient altbit.org blog back on July 7th, 2007. I used to be a frequent user of dc but in recent years I've replaced it with galculator. Back then I used to try to stretch the limits of the command where possible. I discovered this obvious but useful feature in the process.

It was also around this time that I was running World of Warcraft on FreeBSD. Those were some wild days. Perhaps I'll post about some of that in the future as well.


Filed under General Tech

I frequently use dc for quick calculations, but it’s default scale is set to 0. This can be annoying if you’re dealing with lots of decimals.

The k parameter sets the scale and it’s as simple as typing nk, where in is the precision you desire. For example, to round results to 2 decimal places, you’d type 2k.

Technically the k parameter pops the top value off the stack and uses it as the precision, but placing a number in front of it sets that number to the top of the stack.

See the parameters section in man 1 dc for more details, there are some cool operators in there.