Jump to content

Round of into two decimal places


newphpcoder

Recommended Posts

Hi..

 

I encountered problem in rounding of numbers into two decimal places.

 

here is my sample code:

 

if($W4_STATUS == 1 AND $DEPENDENTS == 0 AND $TotEarn >= 7917 AND $TotEarn <= 12500) {
   $TAX = ($TotEarn - 7917);
   $TAX = (937.50 + ($TAX * .25));
   $TAX = number_format($TAX, 2, '.', '');
}

 

for example from this:

$TAX = ($TotEarn - 7917);
  $TAX = (937.50 + ($TAX * .25));

the output is: 1417.615

 

using this:

$TAX = number_format($TAX, 2, '.', '');

the output was : 1417.61

 

but it should be : 1417.62

 

Thank you

Link to comment
https://forums.phpfreaks.com/topic/261151-round-of-into-two-decimal-places/
Share on other sites

if($W4_STATUS == 1 AND $DEPENDENTS == 0 AND $TotEarn >= 7917 AND $TotEarn <= 12500) {

$TAX = ($TotEarn - 7917);

$TAX = (937.50 + ($TAX * .25));

//printf('%0.5f; %s', $TAX, number_format($TAX, 2, '.', ','));

$TAX = number_format($TAX, 2, '.', '');

//$TAX = round($TAX, 2);

}

 

$TAX = (9837.46 - 7917);

$TAX = (937.50 + (1920.46 * .25)); // (937.50 + 480.115 )

$TAX = number_format(1417.615, 2)

 

I tried some testing:

$TAX  = ($TotEarn - 7917); // 1920.46

$TAX = ($TAX * . 25); // 480.115

 

$TAX = round($TAX, 2) // 480.11

 

but when I tried this:

 

$TAX = (480.115);

$TAX = round($TAX , 2); // 480.12

 

I notice that if the variable is  = actual numeric it works but if it is came from variable it did not work :(

 

I really don't know what to do :(

 

Thank you

 

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.