Getting only 1 decimal place

Question:

How do I convert 45.34531 to 45.3?

Asked By: Alex Gordon

||

Answers:

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
Answered By: relet
round(number, 1)
Answered By: DixonD
>>> "{:.1f}".format(45.34531)
'45.3'

Or use the builtin round:

>>> round(45.34531, 1)
45.299999999999997
Answered By: miku
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.