PDA

View Full Version : Round of into two decimal places

newphpcoder
04-18-2012, 04:53 AM
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

Fou-Lu
04-18-2012, 05:04 AM
\$t = 1417.615;
print number_format(\$t, 2);

I got 1417.62 when given 1417.615.

mistry.sandi009
04-18-2012, 05:39 AM
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

newphpcoder
04-18-2012, 05:49 AM
\$t = 1417.615;
print number_format(\$t, 2);

I got 1417.62 when given 1417.615.

Yes... I'm sure

Fou-Lu
04-18-2012, 05:59 AM
Yes... I'm sure

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

What are the results?

Make sure that is done before your number_format.

newphpcoder
04-18-2012, 06:39 AM
printf('%0.5f; %s', \$TAX, number_format(\$TAX, 2, '.', ','));

What are the results?

Make sure that is done before your number_format.

the result is :

1417.61500; 1,417.61

Fou-Lu
04-18-2012, 06:51 AM
>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?

newphpcoder
04-18-2012, 08:19 AM
>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

Fou-Lu
04-18-2012, 05:07 PM
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.