I need to insert a float value that I get from the user to the sqlite3 database. For this, I am using sqlite3_bind_double() function. The problem is that even if I enter a value of 23.3 in the database I still get a long double as 23.3999615. How can I limit the number to only have two decimals to be stored in the database?
Asked
Active
Viewed 183 times
0
-
`long` and `long double` are _binary_ floating point types. "limit the number to only have two _decimals_" is not a property of those types. – chux - Reinstate Monica Jan 01 '20 at 00:04
-
Is that truly 23.3 and not 23.4 in the example that made 23.3999615? – chux - Reinstate Monica Jan 01 '20 at 00:05
-
@chux-ReinstateMonica I understand, but can't find any other function for particularly float binding – MaG Jan 01 '20 at 01:20
-
@chux-ReinstateMonica as far as I remember it was 23.3, I didn't save the input anywhere so might be mistaken – MaG Jan 01 '20 at 01:21
-
1The closest ieee754 double to 23.4 is 23.3999996185302734375, which is what sqlite would store internally. So if you meant 23.4 and not 23.3, that result makes sense. See [Is Floating Point Math Broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Shawn Jan 01 '20 at 02:06
-
@Shawn yes, but it's not the point of my question... I want to store number with two decimals only, but couldn't find any other way rather than using bind_double – MaG Jan 01 '20 at 19:48
-
Just round it to the desired precision when formatting as a string for display? – Shawn Jan 02 '20 at 00:46