0

I would like to truncate a floating point number at the third decimal place. However, I don't want to round the number.

float("{0:.3f}".format(132.32579))

The code above produces 132.326 but I want it to produce 132.325. Any help is appreciated

AKSuited
  • 55
  • 3

1 Answers1

0

You can round to 1 digit more, slice the string for ignoring the last digit and convert back to float:

print( float("{0:.4f}".format(132.32579) [:-1]))

You can avoid the string conversion/rounding by multiplying by 1000, converting to int and dividing by 1000.0 again:

print( float(int(132.32579*1000)/1000.0))

Output (both):

132.325
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69