2

I am trying to do simple calculation using bc command in linux. Once i want the value with all the decimal places. other time i want only the integer part

Want to get with full decimal places

$ echo "scale=10; ((900/1303) * 928)/600" | bc
1.0683039140

Now i want to get only the 1 (integer) part here omitiing the decimal part SO i try

$ echo "scale=0; ((900/1303) * 928)/600" | bc
0

How its showing Zero. It should show 1 instead

Can someone help. I dont want to extract the interger part using awk or sed in this case

1 Answer 1

6

Try:

echo "scale=20; a=((900/1303) * 928)/600; scale=0; a/1" | bc

However, that's a truncate decimals, a result like 1.9999 will truncate to 1 also.

But that seems to be what you ask for.


How its showing Zero. ...

Because 900/1303 (with zero decimals) becomes 0. The 0 is carried to the end result. Maybe, if you reorder:

$ echo "( 900 * 928 / 1303 ) / 600" | bc
1

... It should show 1 instead

No, it should not, if intermediate divisions are carried out with zero decimals.

1
  • yes : ( 900 * 928 / 1303 ) / 600 will work.
    – Santhosh
    Commented Dec 14, 2019 at 19:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .