What is the best way to store people's current balance in MySQL? I'm developing something like Neobux, so each user have a balance data in MySQL..
It can be tempting to store a value such as $10 as 10.00 in your database. However, storing monetary values with a decimal point can cause a lot of issues. Instead, you should always store monetary values in their minor unit form. So to record $10, you would save it as an integer in your database as 1000 .
The DECIMAL and NUMERIC types store exact numeric data values. These types are used when it is important to preserve exact precision, for example with monetary data.
We can store the money values in MySQL in decimal(value1,value2). Here, value1 is the total range including value2. The value2 specifies the number of digits after the decimal point.
The purpose of every database is to store information, texts, images, even media files. All dynamic modern websites rely on one or more databases for storing articles and other published content, information about the users, contact information, connections to other websites, ads, etc.
Use decimal(10,2)
. float
is susceptible to rounding errors, see here.
For further information, you can take a look at these pages:
I say store all of the transactions rather than just the current balance. That way you have history, and you can run different aggregates on it rather than just "final balance."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With