Php value rounding is different from VB (Visual Basic)

I am converting a vb application to a php application. It is all about financing. This is where I get the problem; in fact, the method of rounding values ​​in php is different from the vb application.

We store the values ​​of 14 accounts and the distribution amount. In this 5 of them have 0.01 discrepancies. We have listed below a table of 14 accounts with their distribution volume.

β€’ Account Number

β€’ Before applying the round function, the actual value of the distribution amt

β€’ Rounding of distribution volume in VB

β€’ Rounding up the distribution volume in PHP

enter image description here

From this it can be seen that the value of 0.01 discrepancy is due to the conflict behavior of the round function between VB and PHP. How to solve this problem, I need the same amount as VB.

+5
source share
2 answers

When a fraction is .5 VB6 is rounded to the nearest, while PHP is rounded.

There is an optional third mode parameter for the round function in PHP, the default is PHP_ROUND_HALF_UP - you want PHP_ROUND_HALF_EVEN .

Example:

 echo round(29.205, 2, PHP_ROUND_HALF_EVEN); // 29.2 

See the php.net documentation for more information on the mode parameter.

+5
source

You will probably find the old How to perform custom rounding procedures , useful for understanding things and taking appropriate action.

0
source

Source: https://habr.com/ru/post/1200336/


All Articles