Closed (fixed)
Project:
Simple Payments
Version:
6.x-1.x-dev
Component:
Code
Priority:
Major
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
3 Nov 2010 at 00:17 UTC
Updated:
13 Dec 2010 at 11:10 UTC
Jump to comment: Most recent file
Comments
Comment #1
seanberto commentedComment #2
seanberto commentedAlrighty, here's a patch for this. It's a bit funky b/c it's applied against the patches in: #839952: Add payer_reference and payee_reference to simple_payment table and #869142: Add views integration. Gosh a lot of this work needs to get into the stable release of this module....Love to help however I can.
Comment #3
jhedstromSubscribing.
Comment #4
jbrown commentedhttp://drupal.org/cvs?commit=454924
Comment #5
jbrown commentedI think this might have been a mistake.
The advantage of storing the amount as an integer is that you can use normal PHP maths operators. PHP doesn't have a native decimal variable type, so you have to use something like http://php.net/manual/en/book.bc.php to access / manipulate your data.
Comment #6
seanberto commentedI guess that I don't understand the issue. You can present numbers as decimals (or as currency) with standard PHP functions. Can you help me better understand by providing an example scenario where this would cause problems? With the patch I'm using simple payments with Views Calc to provide aggregate payment reports and everything's working okay.
I think that the most obvious problem with storing this data as an integer multiplied by 100 is that it's unintuitive and requires that other modules that hook into yours consistently manipulate the data to make it behave as expected. For example, the fact that you were storing integers makes the view handlers all more difficult.
Comment #7
jbrown commentedThe problem is that PHP will treat the amount as a float, but floats can't store base 10 decimals - rounding errors will creep in unless you use bcmath extension.
Putting in a decimal point can be done in the theme layer.
Comment #8
seanberto commentedHuh, Ubercart and the Payments modules both store dollar amounts as floats or decimals in the DB. I don't see the logic just yet re: when they swap back and forth - Ubercart seems to use both in different places. Of the two, it seems to me that a decimal is preferable over a float for the reasons that you've brought up.
Regardless of how you store the value in the DB, it seems like you're always going to have to convert amounts to integers for PHP to handle the math precisely. And even then, you are going to have to be careful that you don't lose precision when PHP tries to convert integers to floats when you do division (see: http://www.php.net/manual/en/language.operators.arithmetic.php). With this in mind, it doesn't seem like you're getting around any of this extra work by saving integers in the DB.
If anything, the DB is the only place where currency can be stored/manipulated sanely as a decimal. Why give that up?
Comment #9
jbrown commentedOkay - I'll leave it as is, but make sure you use bcmath for all mathematical operations on amount.