if number is 0.1 then I want it to be 1.0 and same for all the numbers. if a number has something in decimal place then I want to round it off to next digit.
2 Answers
Use math.ceil:
python 2:
>>> import math
>>> math.ceil(0.1)
1.0
python 3:
>>> import math
>>> float(math.ceil(0.1))
1.0
Thanks to @PM 2Ring for pointing out the difference between python2 and python3.
3 Comments
PM 2Ring
Note that in Python 3
math.ceil() returns an int, but in Python 2 it returns a float.Karun Madhok
can you tell why it is returning float in python 2 and int in python 3?
Leistungsabfall
This answer speculates that it may have sth. to do with the introduction of long ints. But I really don't know if that's the real reason.
you can define a lambda function that do the job.
>>> myround = lambda x: float(int(x)) if int(x) == x else float(int(x) + 1)
>>> myround(0.1)
1.0
>>> myround(2.0)
2.0
1 Comment
BDL
Although this code segment might answer the question, one should add an explanation to it.