If we want to round 4.732 to 2 decimal places, it will either round to 4.73 or 4.74. 4.732 rounded to 2 decimal places would be 4.73 (because it is the nearest number to 2 decimal places). 4.737 rounded to 2 decimal places would be 4.74 (because it would be closer to 4.74).
You should just write "{:. 1f}". format(45.34531) .
Examples on rounding off to correct one place of decimal or rounding off to the nearest tenths: (i) 14.732 → 14.700. We see the digit in the hundredths place is 3 then round it to the nearest tenths which is smaller than the given decimal number. Since 3 < 5 then the decimal number is rounded to 14.700.
Are you trying to represent it with only one digit:
print("{:.1f}".format(number)) # Python3
print "%.1f" % number # Python2
or actually round off the other decimal places?
round(number,1)
or even round strictly down?
math.floor(number*10)/10
>>> "{:.1f}".format(45.34531)
'45.3'
Or use the builtin round:
>>> round(45.34531, 1)
45.299999999999997
round(number, 1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With