Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing currency values in MySQL database

Tags:

mysql

currency

This question has been asked many times before, but I've found conflicting opinions on the topic so I thought I would bring it up again in hopes of a more unified conclusion.

I would like to store a currency value in my database. Let's assume all entries are the same type of currency (USD for example) and that both positive and negative values are allowed.

My initial thought would be to store the value as a signed integer in terms of the smallest unit of the associated currency. For example, if I want to store the value $1.25, I would insert 125 into the database, since the smallest unit of USD is $0.01. The nice thing about this method is that MySQL will automatically round to the nearest integer. For example, if the dollar value is $1.259, I could insert 125.9, which would automatically be rounded and stored as 126 or $1.26.

So, what do you think? Is this a sound approach or is there a better way?

like image 647
David Jones Avatar asked Jun 08 '12 16:06

David Jones


People also ask

What data type is best for currency in MySQL?

From the MySQL manual: 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.

Which database is best suited to store currency values?

Which datatype is best suited to store currency values? Explanation: Currency is a numeric information. For monetary calculations, FLOAT and DOUBLE are subject to rounding error and may not be suitable. A DECIMAL(M, 2) type is best suited for it.

How do you store dollar amounts in a database?

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 .

Which datatype is used to store currency values?

The MONEY data type stores currency amounts. TLike the DECIMAL(p,s) data type, MONEY can store fixed-point numbers up to a maximum of 32 significant digits, where p is the total number of significant digits (the precision) and s is the number of digits to the right of the decimal point (the scale).


1 Answers

Financial data can be stored as DECIMAL(p,s) where p is the number of significant digits, and s is the scale (number of places to the right of the decimal point). See The MySQL documentation.

Also from O'Reilly's High Performance MySQL, chapter 3:

"...you should use DECIMAL only when you need exact results for fractional numbers--for example, when storing financial data."

From O'Reilly's Learning MySQL:

DECIMAL is, "...A commonly used numeric type. Stores a fixed-point number such as a salary or distance..."

And MySQL Pocket Reference:

DECIMAL "...Stores floating-point numbers where precision is critical, such as for monetary values."

like image 50
DavidO Avatar answered Nov 03 '22 12:11

DavidO