0

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 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Note that in Python 3 math.ceil() returns an int, but in Python 2 it returns a float.
can you tell why it is returning float in python 2 and int in python 3?
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.
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

1 Comment

Although this code segment might answer the question, one should add an explanation to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.