user1746291 - 4 years ago 180

Python Question

I want to round off some values for my numerical calculations in views like-------

5.5 to 5

OR 549 t0 500 and 599 - 600 (the nearest one)

So I want to use round off OR floor function in django as we used in other languages. Is there any functions related to this in django then please suggest me some functions and also the libraries which i have to import to use these functions.

Thanks.

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

Use `decimal`

in your view:

```
>>> import decimal
>>> numbers = ['5.5','499','1','0.5']
>>> for i in numbers:
... the_number = int(decimal.Decimal(i))
... if the_number / 10:
... the_number = the_number + 10 - (the_number % 10)
... print the_number
...
5
500
1
0
```

If you want to format floats, use `floatformat`

as suggested by Daniel, but it won't do the rounding for you.

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**