0

I'm building a web application with CakePHP. This application will use lots of financial data, and perfect accuracy is required.

As you may now, floats shouldn't be used when working with money/currency/financial data (Why not use Double or Float to represent currency?).

In Delphi I know I can use the Currency datatype, in Java I can use BigDecimal and in .NET there is the Decimal datatype, but which datatype or functions must I use with CakePHP? What should I do to avoid rounding and accuracy problems? Must I store all my data as integers and divide by 100 when displaying it?

Thanks for your help!

Community
  • 1
  • 1
marc casas
  • 133
  • 2
  • 9
  • 2
    That's more of a PHP problem: http://stackoverflow.com/questions/14587290/can-i-rely-on-php-php-ini-precision-workaround-for-floating-point-issue – ndm Nov 22 '13 at 22:02
  • So the only options are using integers (and divide when displaying) or change to another framework and language, isn't it? – marc casas Nov 22 '13 at 23:08
  • @marccasas If floats are not precise enough for your needs, then yes, you'd need to look at a language that has a more precise number representation. – Oldskool Nov 23 '13 at 11:30
  • Most of us just use "decimal" for this in MySQL. And round at output to the required accuracy. – mark Nov 23 '13 at 13:09
  • @mark, I know I must use the decimal datatype in MySQL. My concerns are about what to use in PHP... – marc casas Nov 23 '13 at 16:46
  • @Oldskool: no, floats are NEVER precise enough when working with money. With money/currency values you must use a fixed point datatype, like the Decimal datatype form SQL, the Currency from Delphi or the BigDecimal from Java. It isn't really a problem about precision, the real problem is there is numbers in decimal that hasn't exact representation when using float numbers. – marc casas Nov 23 '13 at 16:49

0 Answers0