106

bc handles numbers as integers:

# echo "100/3" | bc
33

bc -l handles numbers as floating point objects:

# echo "100/3" | bc -l
33.33333333333333333333

Is there a way to limit the number of digits after the decimal point?

muru
  • 197,895
  • 55
  • 485
  • 740
Adam Matan
  • 12,519
  • 13
    Interestingly, this only works with division. If you want to do scale=0;1234*1.1, you have to write it as scale=0;1234*1.1/1 to get 1357. Otherwise, no matter the value of scale, you get 1357.4. – Wok May 12 '14 at 13:37
  • 3
    @Wok, it's not dependent on division or multiplication. It depends on the input - the biggest precision number determines the precision in the output. Notice how 1234*1.0 will give you 1234.0 – Richlv Aug 17 '16 at 18:53

5 Answers5

129

Set the scale special variable:

$ echo "scale=2; 100/3" | bc
33.33
chronitis
  • 12,367
  • 4
    See @Wok's comment on the question. – Adam Matan Apr 28 '16 at 11:59
  • 1
    $ echo "scale=2; (100/180)*180" | bc gives 99.00 :( – Donatas Olsevičius Sep 09 '16 at 10:39
  • 1
    @DonatasOlsevičius this is because (100/180) = 0.55 and then (0.55 * 180) = 99. So it is giving you right value :) – Kamaldeep singh Bhatia Feb 07 '17 at 13:22
  • It would be nice if it rounded up if above .5.
    ~$ echo "scale=2; 12/104" | bc .11

    If rounded up this would be .12. However, it should still do the job for my task.

    – jbrock Aug 29 '17 at 16:01
  • 10
    You can maintain precision until printing the value this way: echo "result = (100/180) * 180; scale=2; result / 1" | bc -l. Now you get 99.99. – Byron Hawkins Aug 13 '18 at 01:44
  • I get it to round up if the fractional part is over 0.5 using e.g. bc -l <<<'a=20/3;scale=0;(a+0.5)/1' This returns 7 because 20/3 is 6⅔ but using scale=0 would make it 6 because of the weird rounding. Adding 0.5 means it will always be over the next integer if the fractional part of the result was already at least 0.5 -- hence, it'll effectively round up if the fractional part is 0.5 or more. PS It's important to use bc -l not just bc. – IpsRich Dec 02 '21 at 16:12
  • It does not work in case: echo "scale=1; -0.000004964112 + -3.988281" | bc. It prints -3.988285964112, while -3.9 (or near) is expected because the scale is 1. Why? How to fix? – pmor Sep 05 '23 at 09:54
22

scale works only for division; if some geeks need it in multiplication, then you can do achieve this by using string manipulation. Say if you need to multiply 32 * 0.60 , answer is 19.20. If you need to get it 19 alone in answer you can get it by different methods.

  1. Using String Manipulation

    $ S=$(echo "32*.60" | bc ) ; echo ${S%.*}
    19
    

    String Manipulation syntax: ${Variable%pattern}, this will delete short matching pattern that comes after %. For more String manipulation details see the Advanced Bash-Scripting Guide.

  2. Using Scale as stated by **chronitis**

    $ echo "scale=0; 32*60/100" | bc
    19
    
  3. To get rid of the trailing 0s, instead of string manipulation, one can also do a divide by 1.

    $ echo "0.232 * 1000" | bc
    232.000
    
    $ echo "0.232 * 1000 / 1" | bc
    232
    
SAGAR Nair
  • 1,385
  • 1
    Note that, as i mentioned in my comment on the question, it is not caused by multiplication, but rather by the input numbers having a decimal part. Or, to put it differently, the number with the "longest" decimal part will determine how many decimal places the output will have. – Richlv Aug 17 '16 at 18:55
  • 2
    Thanks in particular for the hint that division by 1 gets rid of the decimal portion! – porg May 06 '22 at 18:41
9

you can also use printf command to round off result upto 3 decimals

# printf "%.3f\n" $(echo "100/3" | bc -l)
3.333
6

In addition to previous answers

echo "scale=2; 1.0150876" | bc

Returns

1.0150876

Add Math operations to get only 2 decimal numbers - (NUMBER*100)/100

echo "scale=2; (1.0150876 * 100) / 100" | bc

Now returns

1.01
3

Round-off

scale=2 truncates the answer to two decimal digits, but we can achieve round-off like so:

$ echo "a=12/104; scale=2; (a+0.005)/1" | bc -l
.12
spanky
  • 103
  • 1
  • 2
  • 10