Ubuntu – Command line calculator that keeps fractional values

bccalculatorcommand line

I just found a good command line calculator program called bc and was satisfied with it until I discovered it rounds off fractional values, thus causing loss in precision.

According to its man page:

All numbers are represented
internally in decimal and
all computation is done in decimal. (This version truncates
from divide and multiply operations.)

Could you please suggest an equivalent of bc for Ubuntu Maverick? I need to make advanced command line calculations with variables.

Best Answer

  • You can set the length of the fractional part with scale=n.

    The command echo 'scale=20;752/447' | bc yields:


    Note that even if the number fits within the scale, additional zero's might be appended:


    Unfortunately, there is always a rounding issue: