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

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.

  • yes : ( 900 * 928 / 1303 ) / 600 will work. – Santhosh Yedidi Dec 14 '19 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.