Sqlite How To Use Integer For Storing Price Value
Solution 1:
It is actually quite simple when you see it.
I don't know what currency you are in, but assume US $.
You want to store $1.01. This is the same as 101 cents. You just multiply by 100.
So before storing the number, multiply by 100. When displaying the number, divide by 100.
Just to add to that answer: I also think you should store the numbers in memory as cents/integers too. So for all your workings, calculations etc, use that integer format.
If a user enters a $ amount, convert it straight away into integer (multiply by 100). And only change back to $ when you need to display it.
In other words, not just when going to and from the database.
Solution 2:
Effective Java book describes, in Item 48, that one should not use float or double if exact answers is required.
I think you should use long value to store the price value, but store the values in "cents" (or equivalent as per currency of choice) instead of "dollars". This way your math around prices should work just fine with no loss due to floating-point arithmetic.
Post a Comment for "Sqlite How To Use Integer For Storing Price Value"