I have a unicode string x = u'12345678.87654321'
, I want to convert it to float
in python using
float(x)
It is converted to 12345678.88
instead. It seem like float()
automatically rounds the number to two decimal places. I want to keep whatever is in the unicode string (less than 10 decimal places). What would be a good alternative?
EDIT: My apologies. The example I used is not tested. I will just use my real data: I have an unicode string u'1464106296.285190'. This is the one that cannot be converted to float and retain all decimal places.
Use a decimal.Decimal
:
In [103]: import decimal
In [104]: D = decimal.Decimal
In [109]: D(u'1464106296.285190')
Out[109]: Decimal('1464106296.285190')
In [110]: float(u'1464106296.285190')
Out[110]: 1464106296.28519
In [111]: print(D(u'1464106296.285190'))
1464106296.285190
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With