Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does python provide wrong calculations with float

Tags:

python

x = '16473.6'
y = str(int(float(x) * 1000))
print(y)

>>>16473599

OK... obviously I did something wrong... i know that these float thing are sometimes a little bit difficult from c#... but i thought that * 1000 should work... OK wrong

Can someone tell me how to do this better?

thank you very much

like image 414
user2111880 Avatar asked Aug 21 '14 06:08

user2111880


1 Answers

Floating point numbers have always a problem with calculation as it's based on a binary approximation of numbers.

You may check Floating Point Arithmetic: Issues and Limitations

You may try using Decimal

x = '16473.6'
y = str(int(Decimal(x) * 1000))
print(y)
like image 72
Rahul Tripathi Avatar answered Oct 02 '22 14:10

Rahul Tripathi