Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Round of into two decimal places

1. ## 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

• PHP Code:
``` \$t = 1417.615;print number_format(\$t, 2);  ```
I got 1417.62 when given 1417.615.

• Hi,

if u r getting value like this 1417.61 and display it using number_format then u will get the result same but,
if u r getting value like this 1417.615 and display it using number_format then u will get the result like 1417.62

so kindly check the decimal points u getting.

Regards,
mistry Sandip

• Originally Posted by Fou-Lu
PHP Code:
``` \$t = 1417.615; print number_format(\$t, 2);  ```
I got 1417.62 when given 1417.615.
Yes... I'm sure

• Originally Posted by newphpcoder
Yes... I'm sure
PHP Code:
``` printf('%0.5f; %s', \$TAX, number_format(\$TAX, 2, '.', ','));  ```
What are the results?

Edit:
Make sure that is done before your number_format.

• Originally Posted by Fou-Lu
PHP Code:
``` printf('%0.5f; %s', \$TAX, number_format(\$TAX, 2, '.', ','));  ```
What are the results?

Edit:
Make sure that is done before your number_format.
the result is :

1417.61500; 1,417.61

• Code:
```>php -r "\$TAX=1417.61500; printf('%0.5f; %s', \$TAX, number_format(\$TAX, 2, '.', ','));"
1417.61500; 1,417.62```
I can't replicate this number. What version of PHP are you using?

• Originally Posted by Fou-Lu
Code:
```>php -r "\$TAX=1417.61500; printf('%0.5f; %s', \$TAX, number_format(\$TAX, 2, '.', ','));"
1417.61500; 1,417.62```
I can't replicate this number. What version of PHP are you using?
php 5.5

• 5.5?
You'll need to report this as a bug then since 5.5 wouldn't exist beyond a testing stage (if at all, I can't find a snaps for it). I believe to date the highest versions available in the 5 family are 5.4.1RC2 and 5.3.11RC2.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•