Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store a floating point number as text without losing precision?

Like the question says. Converting to / from the (truncated) string representations can affect their precision. But storing them in other formats like pickle makes them unreadable (yes, I want this too).

How can I store floating point numbers in text without losing precision?

like image 337
Jesvin Jose Avatar asked Jun 20 '12 06:06

Jesvin Jose


1 Answers

Store it in binary or a power thereof.

>>> (3.4).hex()
'0x1.b333333333333p+1'

>>> float.fromhex('0x1.b333333333333p+1')
3.4
like image 181
Ignacio Vazquez-Abrams Avatar answered Oct 22 '22 01:10

Ignacio Vazquez-Abrams