How to Store Decimal Currency Values in MySQL

Now while storing integers and numerals in MySQL is pretty rock solid and easy to do, storing decimal values like currency, in other words money, isn’t quite as intuitive when you have a phpMyAdmin structure tab open in front of you.

When it comes to storing non integer numbers, you pretty much have the choice of taking either the FLOAT or DECIMAL route. Now FLOAT is intended for very large values and rounds as a floating point, which is a bit of a problem at times because floating point arithmetic is not entirely exact, particularly when it comes to rounding – which is definitely not a behaviour you want went it comes to adding and subtracting all those cents in any of your financial applications.

DECIMAL was introduced as an “exact packed decimal number”, but prior to MySQL 5.0, any calculations performed on a DECIMAL type column would be done using floating point arithmetic, which didn’t really solve the problem. However, starting with MySQL 5.0 and above, the server now uses a separate fixed-point arithmetic library for this, making it far more precise and far less prone to rounding errors.

Thus the best way to currently store your money value in whatever currency, be it in Rands or US Dollars is to declare a DECIMAL column type and assign it a length/value of 10,2 (where 2 indicates the length of the fractional part of the number).

(Of course you could always be a Smart Alec and simply save everything as cents in the first place)

You might also enjoy:

  • PHPMyAdmin is a useful web-based management tool for your MySQL server running as part of your LAMP stack. To install this tool in Ubuntu turns out to be pr ...
  • By default, most MySQL installations' root user account starts with a blank password, a password set to nothing. Now when installing MySQL as part of a pack ...
  • That's what I love about Google. They're like Microsoft, only going about their grab for absolute power more covertly, making the masses love them instead o ...
  • LAMP (Linux, Apache, mySQL and PHP) is a popular open source web development platform that uses Linux as its operating system, Apache as the web server, myS ...
  • To get a list of user accounts on a MySQL server instance, we need to make a SELECT call to the mysql.user table. First, fire up MySQL in your terminal ...

About Craig Lotter

Craig Lotter is an established web developer and application programmer, with strong creative urges (which keep bursting out at the most inopportune moments) and a seemingly insatiable need to love all things animated. Living in the beautiful coastal town of Gordon's Bay in South Africa, he games, develops, takes in animated fare, trains under the Funakoshi karate style and for the most part, simply enjoys life with his amazing wife and daughter. Oh, and he draws ever now and then too.
This entry was posted in Technology & Code, Tutorials and tagged , , float, , , , . Bookmark the permalink.
  • http://www.francoisharris.com/ Francois

    Hey,

    I am testing a db on my computer before I upload, and am using DECIMAL (6,2). The values for the prices I insert seem to always have the cents set to zero, so when I put in the value 150.95 the price I get when displaying the data is R150.00. Is this because of the mysql version, or could it be something else?
    .-= Francois´s last blog ..Another website goes live =-.

  • http://www.francoisharris.com Francois

    Hey,

    I am testing a db on my computer before I upload, and am using DECIMAL (6,2). The values for the prices I insert seem to always have the cents set to zero, so when I put in the value 150.95 the price I get when displaying the data is R150.00. Is this because of the mysql version, or could it be something else?
    .-= Francois´s last blog ..Another website goes live =-.

  • http://www.craiglotter.co.za Craig Lotter

    Hi Francois. Which version of MySQL are you using and what are the SQL statements being used to insert the values into the table?

  • http://www.craiglotter.co.za Craig Lotter

    Hi Francois. Which version of MySQL are you using and what are the SQL statements being used to insert the values into the table?

  • http://www.forexfundamentalanalysis.net/dec-met-ready-reckoner-for-decimal-currency-values-of-kilogrammes-and-tonnes Dec-Met Ready Reckoner for Decimal Currency Values of Kilogrammes and Tonnes

    [...] How to Store Decimal Currency Values in MySQL [...]

  • http://richardcummings.info Richard Cummings

    Craig, Thx for posting the screenshot of adding a decimal data type in phpadmin…just what I was looking for. Cheers, Richard

  • http://www.grumpyoldgamers.net/ Kurt Goodliff

    Cheers for that, I somehow missed the DECIMAL type when looking through the drop down :(

blog comments powered by Disqus