Karun Madhok
Karun Madhok

Reputation: 91

How to round off a float values in django-python?

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.

Upvotes: 0

Views: 1601

Answers (2)

Aboud Zakaria
Aboud Zakaria

Reputation: 567

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

Upvotes: 0

Leistungsabfall
Leistungsabfall

Reputation: 6488

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.

Upvotes: 5

Related Questions