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.
Asked
Active
Viewed 1,427 times
2 Answers
5
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.

Leistungsabfall
- 6,368
- 7
- 33
- 41
-
2Note that in Python 3 `math.ceil()` returns an `int`, but in Python 2 it returns a `float`. – PM 2Ring Aug 18 '15 at 08:44
-
can you tell why it is returning float in python 2 and int in python 3? – Karun Madhok Aug 18 '15 at 09:55
-
[This answer](http://stackoverflow.com/a/9180047/3224008) 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. – Leistungsabfall Aug 18 '15 at 17:12
0
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

Aboud Zakaria
- 567
- 9
- 25
-
Although this code segment might answer the question, one should add an explanation to it. – BDL Aug 18 '15 at 09:34