views:

56

answers:

2

How to properly save a numeric values into SQLite3. I've used Real type for storing numeric value. For example: when I store 233333.00 is fine. But when i store 23333333.00 it becomes 23333300.00. Why is it saved as 2.3333333E7 and retrieved as 2.33333e+07. Is there a way to overcome this?

+2  A: 

The real type used by SQLite is stored as 8-byte IEEE floating point number and can't store all values exactly. If you want exact vales, use either string of integer.

Chandra Patni
Thanks. Any idea what sort of data field would be best to use REAL type?
ychian
There are no *real* type in physical world. If you need to store money, then you can use integer field (in cents). If you need exact strings back with arbitrary precision then you can string. You can use real if you can live with some loss of precision.
Chandra Patni
A: 

You are losing precision because your program's variable has single precision and you need double.

Floating point is actually perfectly accurate for integral values that fit in the mantissa.

You get 24 bits fot singles and 53 for double.

This isn't an sqlite issue at all.

DigitalRoss