I am converting a vb application into php application It is all about financing. In that, I am getting an issue; actually the way of rounding the values in php is different from vb application.
We are storing the values of 14 accounts and distribution amount. In this 5 of them are having 0.01 discrepancies. We have given below a table of 14 accounts with their distribution amount.
• Account Number
• Before applying round function, Actual value of distribution amt
• Distribution Amount Rounding in VB
• Distribution Amount Rounding in PHP
From that we can see the value of 0.01 discrepancy is occurring due to the conflict behaviour of round function between VB and PHP. How to resolve this issue, I need the same amount as like VB.
When the fraction is .5 VB6 is rounding to nearest even whereas PHP is rounding up.
For the round
function in PHP there is an optional third parameter, mode
, which defaults to PHP_ROUND_HALF_UP
- you want PHP_ROUND_HALF_EVEN
.
Example:
echo round(29.205, 2, PHP_ROUND_HALF_EVEN); // 29.2
See php.net documentation for more information on the mode
parameter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With