I need to find the way for converting the Float value in to the two point decimal.
I have a API code which is implemented already which will be returning the two Float value.
For example consider it is returning the values as follows: 293.019999504
But I need to make it as 293.01 instead of 293.019999504
As well as it should be handling 0 as 0.00
I am unable to modify the API implementation backend or DB tables.
I need to implement this in views.py where I am getting the values using the API calls.
Need a way to achieve this in pythonic way.
答案 0 :(得分:3)
You should do as follows:
a = 293.019999504
print("{0:.2f}".format(a))
>> 293.02
答案 1 :(得分:0)
IIUC you want to use the round function:
round(293.019999504,2)
Out[1]:
293.02
Note that it will round your number (so 293.019 will end up as 293.02), if you don't want that, you can use floor:
from math import floor
floor(100*293.019999504)/100
答案 2 :(得分:0)
如果你真的想要向下舍入,你需要像
这样的东西 int(293.019999504 * 100) / 100.0
但是,处理会计事项的正确方法是定点计算,如下所述:https://docs.python.org/2/library/decimal.html。