Home > Software design >  How to round to 1 decimal in bash?
How to round to 1 decimal in bash?

Time:03-07

I am trying my first script on bash and I am not able to round (not truncate).

So, I am getting 55.08 and I would like to convert it to 55.1

promedio=$(echo "scale=2; $lineas/($dias*24)" | bc)

Note that I am currently using scale=2 because scale=1 produces 55.0.

Thank you all :)

CodePudding user response:

This is not so much a bash question as a bc one, but bc doesn’t round. The easiest approach is probably just to add .05 after the division, then set scale to 1 and divide by 1 to force truncation.

  • Related