storing money amounts in mysql

MysqlFloating PointCurrencyFixed Point

Mysql Problem Overview


I want to store 3.50 into a mysql table. I have a float that I store it in, but it stores as 3.5, not 3.50. How can I get it to have the trailing zero?

Mysql Solutions


Solution 1 - Mysql

Do not store money values as float, use the DECIMAL or NUMERIC type:

Documentation for MySQL Numeric Types

EDIT & clarification:

Float values are vulnerable to rounding errors are they have limited precision so unless you do not care that you only get 9.99 instead of 10.00 you should use DECIMAL/NUMERIC as they are fixed point numbers which do not have such problems.

Solution 2 - Mysql

It's not generally a good idea to store money as a float as rounding errors can occurr in calculations.

Consider using DECIMAL(10,2) instead.

Solution 3 - Mysql

Does it really matter if it stores is as 3.5, 3.50 or even 3.500?

What is really important is how it is displayed after it is retrieved from the db.

Or am I missing something here?

Also don't use a float, use a decimal. Float has all sorts of rounding issue and isn't very big.

Solution 4 - Mysql

To store values you can use a DECIMAL(10,2) field, then you can use the FORMAT function:

SELECT FORMAT(`price`, 2) FROM `table` WHERE 1 = 1

Solution 5 - Mysql

Why do you want to store "3.50" into your database? 3.5 == 3.50 == 3.5000 as far as the database is concerned.

Your presentation and formatting of figures/dates/etc should be done in the application, not the database.

Solution 6 - Mysql

If you use DECIMAL or NUMERIC types, you can declare them as for example DECIMAL(18, 2) which would force 2 decimals even if they were 0. Depending on how big values you expect you can change the value of the first parameter.

Solution 7 - Mysql

Binary can't accurately represent floating points with only a limited number of bits. It's not so muuch loss of data but actually conversion errors.. Here's the manual giving examples

You can see this in action in your browser, see for yourself in this code snippet.

<script>

    var floatSum = 0;

    // add 0.1 to floatSum 10 times
    for (var i=0; i<10; i++) {
        floatSum += 0.1;
    }

    // if the repetative adding was correct, the floatSum should be equal to 1
    var expectedSum = 10*0.1; // 1

    // you can see that floatSum does not equal 1 because of floating point error
    document.write(expectedSum + " == " + floatSum + " = " + (expectedSum==floatSum) + "<br />");


    // --- using integers instead ---
    // Assume the example above is adding £0.10 ten times to make £1.00
    // With integers, we will use store money in pence (100 pence (also written 100p) in £1)

    var intSum = 0;

    // add 0.1 to floatSum 10 times
    for (var i=0; i<10; i++) {
        intSum += 10;
    }

    // if the repetative adding was correct, the floatSum should be equal to 1
    var expectedSum = 10*10; // 100

    // you can see that floatSum does not equal 1 because of floating point error
    document.write(expectedSum + " == " + intSum + " = " + (expectedSum==intSum) + "<br />");
    document.write("To display as &pound; instead of pence, we can divide by 100 (presentation only) : &pound;" + intSum/100 + "<br />");
</script>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavidView Question on Stackoverflow
Solution 1 - MysqlMorfildurView Answer on Stackoverflow
Solution 2 - MysqlAndy JoinerView Answer on Stackoverflow
Solution 3 - Mysqlgraham.reedsView Answer on Stackoverflow
Solution 4 - MysqlThiago BelemView Answer on Stackoverflow
Solution 5 - MysqlAndy ShellamView Answer on Stackoverflow
Solution 6 - MysqlMatsTView Answer on Stackoverflow
Solution 7 - MysqlSnellyCatView Answer on Stackoverflow