Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounding a value of php is different from VB(Visual basic)

Tags:

php

rounding

vb6

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

enter image description here

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.

like image 309
Vinayagam Avatar asked Aug 14 '14 15:08

Vinayagam


1 Answers

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.

like image 116
Mitch Satchwell Avatar answered Nov 16 '22 06:11

Mitch Satchwell