Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting only 1 decimal place [duplicate]

People also ask

How do you only get to two decimal places?

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).

How do you limit a float to one decimal place?

You should just write "{:. 1f}". format(45.34531) .

What does correct to 1 decimal places mean?

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)