dmitrirussu / php-sepa-xml-generator

SEPA SDD XML Generator recommended to see. PHP SEPA XML Generator, (Single Euro Payments Area ISO20022 SDD V1_0 20122009) (SEPA)(PHP5)
https://github.com/dmitrirussu/PHP-SEPA-XML-GENERATOR
MIT License
53 stars 36 forks source link

sum of amounts with floats? #9

Closed raimon-segura closed 10 years ago

raimon-segura commented 10 years ago

Hi,

I have been checking the code for "Payment Info total Amount" and I see a little issue: https://github.com/dmitrirussu/php-sepa-xml-generator/blob/master/lib/SEPA/PaymentInfo.php#L440

You made the sum with float variables, best practices said that it must be done with bcadd , that supports fixed point operations.

Another thing is the conversion from float to string, https://github.com/dmitrirussu/php-sepa-xml-generator/blob/master/lib/SEPA/ValidationRules.php#L81

We use http://es1.php.net/number_format function, I think it's equivalent, but I don't know for sure.

To resume, we sum money like this:

// constants $SCALE = 2; $ROUND_SCALE = 2;

// returns formated string $amount_1 = number_format( $float_1, $ROUND_SCALE, '.', ''); $amount_2 = number_format( $float_2, $ROUND_SCALE, '.', '');

// returns string $unformated_sum = bcadd( $amount1, $amount2, $SCALE); // returns float $unformated_rounded_sum = round( $unformated_sum , $ROUND_SCALE); // returns string $sum = number_format( $float_2, $ROUND_SCALE, '.', '');

based on: http://css.dzone.com/books/practical-php-patterns/basic/practical-php-patterns-money

Thanks!

dmitrirussu commented 10 years ago

Thanks Raimon, I have fixed this problem, I added a new method in validation rules which do sum of Operands with bcadd,