# Thread: Round of into two decimal places

1. Registered User
Join Date
Oct 2011
Posts
357

## Round of into two decimal places

Hi..

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

here is my sample code:

PHP 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:
PHP Code:
```  \$TAX = (\$TotEarn - 7917);   \$TAX = (937.50 + (\$TAX * .25));  ```
the output is: 1417.615

using this:
PHP Code:
``` \$TAX = number_format(\$TAX, 2, '.', '');  ```
the output was : 1417.61

but it should be : 1417.62

Thank you

2. Registered User
Join Date
Jan 2006
Location
MN
Posts
440
\$TAX=round(\$TAX,2,PHP_ROUND_HALF_UP);

3. Registered User
Join Date
Oct 2011
Posts
357
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

4. Registered User
Join Date
Jan 2006
Location
MN
Posts
440
\$TAX=round(\$TAX,2,PHP_ROUND_HALF_UP);

or

\$TAX=round(\$TAX,2,PHP_ROUND_HALF_DOWN);

depending on which way you want it to go.

5. Registered User
Join Date
Oct 2011
Posts
357
I resolve my problem in round off into two decimal places by this code:

Code:
```\$TAX = 0.00;

if(\$W4_STATUS == 1 AND \$DEPENDENTS == 0 AND \$TotEarn >= 2083 AND \$TotEarn <= 2500) {
\$TAX = (\$TotEarn - 2083);
\$TAX = (\$TAX * .05);
}
elseif(\$W4_STATUS == 2 AND \$DEPENDENTS == 0 AND \$TotEarn >= 2083 AND \$TotEarn <= 2500) {
\$TAX = (\$TotEarn - 2083);
\$TAX = (\$TAX * .05);
}

...

elseif(\$W4_STATUS == 2 AND \$DEPENDENTS == 4 AND \$TotEarn >= 16667 AND \$TotEarn <= 27083) {
\$TAX = (\$TotEarn - 16667);
\$TAX = (2083.33 + (\$TAX * .30));
}
elseif(\$W4_STATUS == 1 AND \$DEPENDENTS == 4 AND \$TotEarn >= 27083 AND \$TotEarn <= 50000) {
\$TAX = (5208.33);
}
elseif(\$W4_STATUS == 2 AND \$DEPENDENTS == 4 AND \$TotEarn >= 27083 AND \$TotEarn <= 50000) {
\$TAX = (5208.33);
}
else{
}

\$TAX .= 0.00001;
\$TAX = round(\$TAX, 2);
\$TAX_Ded = number_format(\$TAX, 2, '.', '');```
Thank you