Tuesday, 22 August 2017

python: getting only 1 decimal place




How do I convert 45.34531 to 45.3?


Answer



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

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...