Search code examples
phpmysqllaravelcurrencymoney-format

How to handle and/or store money values in web applications?


I'm working with PHP in Laravel 4 and MySQL and I'm not sure how to handle monetary values.

I want to display them, using this format :

  120,50€
1.000,50€

The amount field is of type DECIMAL(12,2).

Should I save the amount like 12050 or like 120,50?


Solution

  • What I would recommend, is to use :

    • decimal Euro values (1000.50) in your database

    • cents (100050) in calculations

    • a formatted string (1.000,50€) in your display to the user


    More details

    For storing a field in a MySQL database, something like DECIMAL(12,2) makes perfect sense. This is a numeric value that has the correct percision (two decimal digits) and allows you to store any monetary value up to 9999999999.99.

    Cents are great for calculations, because it allows you to treat your monetary values as integers in calculations, which is more practical to work with (especially for divisions and multiplications), and which is typically the level you want your monetary values to be rounded up or down to.

    No matter how you store your data in the database or how you do your calculations, you want your users to see the amounts in a format they're familiar with. For example, US dollars would usually formatted as $1,000.50 and Euros as 1.000,50€.