5

I'm building a kind of accounting system with PHP and MySQL. My database has DECIMAL (11,2) columns for currency, and also DECIMALfor all other values used on operations with currency (like percentages to be applied). I've never programmed something like that before, so I am not sure I am doing the right thing.

Am I using the correct level of precision at the database?

On the PHP side:

Should I use BC Math functions on any values that may be later stored on the database?

I also have to format the money values with , as the decimal point, and . as the thousands separator. I know number_format, and recently discovered money_format, but the manual says both operate with floats.

Can I use number_format or money_format for values that will be displayed, or exported to CSV or Excel?

bfavaretto
  • 71,580
  • 16
  • 111
  • 150
  • I could be wrong, but usually financial systems will use at least 3 fraction digits (`DECIMAL(10,3)`). PHP itself is not great with float numbers; the best would probably be to do x100 and use integers only, optionally using BC Math. – Ja͢ck Apr 23 '12 at 03:12
  • Just a quick note - it's usually not a best idea to use floats when working with money. – Ruslan Osipov Apr 23 '12 at 03:23
  • 3
    @pyrate I know, that's why I asked the question. – bfavaretto Apr 23 '12 at 03:51
  • Here is similar question [answered][1] [1]: http://stackoverflow.com/questions/628637/best-data-type-for-currency – Sarath Apr 23 '12 at 05:36

1 Answers1

0

you can use number_format() to display or export the content as csv/excel. But keep in mind while exporting as csv/excel using number_format() with decimal separator as comma(,) you have to use double quotes("). Just like "2,22.00".

Dev
  • 458
  • 1
  • 5
  • 13
  • Thanks, I didn't know that. Anyway I think I'm going to export the values in US formats, and localized copies of Excel should detect automatically, and display the values in the correct format for the current locale. – bfavaretto Apr 25 '12 at 01:56