Hello Ertan,

On 2016-07-05 1:59 PM, Ertan Küçükoğlu wrote:
I have following data to store:

My currency values only has 2 decimal digits. (Sample: 1.23)

My amount values goes around 5-6 decimal digits. (Sample: 1.123456)

So what do the 2 fields actually represent? Are Amount and Money both amounts of currency? If so, why are they stored to different precisions? The field names should be more precise.

If I am to use below SQL command:

CREATE TABLE IF NOT EXISTS ACCOUNTING(

   ID Integer NOT NULL PRIMARY KEY AUTOINCREMENT,

   AmountField float not null,

   MoneyField float not null

);

Will it be fine to store these values, and I will retrieve exactly what I
save?

Stored 1.123456 and retrieved 1.123456 exactly, no rounding or anything.

A float field will never store a fractional decimal value exactly, what it will store is just a binary approximation. However, if the precision you need is low enough relative to the size of the float then it may appear to work.

A key thing here is how you represent the values while they are being transmitted between SQLite and your application. If you are storing them using runtime-bound placeholders rather than literal SQL, and your placeholders are also floats of the same precision SQLite uses, then you can be guaranteed that whatever your application gave to SQLite, it will get back. If the values are inserted as part of the SQL string then it will be rounded.

Its also important to know if you are doing any math with these values in SQL, eg sums, or if you are just storing and retrieving. Inexact values would bite you mainly if you do math.

One thing I cannot do in my case is to store money values as integers
multiplied by 100.

And why not?  That often is still the best advice.

Really its a matter of conception, what is the unit of the currency?

If say your currency was dollars, just say that the field is storing the number of cents rather than the number of dollars, and do all your math assuming it is adding/multiplying/etc counts of cents. Nothing wrong with that. Cents are still just as valid currency in the real world.

Another thing you can do is store the numbers as text fields instead, which works perfectly with situations where you input them literally in a SQL string; in appearance you can put the decimal point whereever you want and the database will remember it exactly, this even works with hundreds of decimal digits or something over the bounds of the numeric types. But if you do math in the database then you may get rounding errors, but if you do math in your app then your app may have an exact numeric type.

I appreciate all advises.

Thanks.

Ertan Küçükoğlu

-- Darren Duncan


_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to