1

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?

John Slegers
  • 45,213
  • 22
  • 199
  • 169
  • 1
    It's actually not a full duplicate as he's asking how to represent it in code as well – Ryall Jul 13 '17 at 17:12

1 Answers1

8

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€.

Community
  • 1
  • 1
John Slegers
  • 45,213
  • 22
  • 199
  • 169
  • Thanks a ton! I have implemented this way and everything it works perfectly for me. Thanks again! – Willfrans Varón Sáenz Mar 07 '16 at 23:35
  • @WillfransVarónSáenz : You're welcome. I'm glad I could help! – John Slegers Mar 07 '16 at 23:36
  • 4
    DECIMAL(12,2) will be OK if all calculations are in Euros. However if other currencies are to be used, it needs to be kept in mind that some currencies don't use 2 decimal places. Some have none, some 3, some 4. https://en.wikipedia.org/wiki/ISO_4217#Active_codes – Votemike Jun 19 '16 at 11:13