/    Sign up×
Community /Pin to ProfileBookmark

number_format/ round

Can anyone explain why is this happening ? number_format() function chooses to round off the second digit to “1” for the first three listed below, but does not do so for the last three. Round() function behaves the same way too.

Number format(10.205) : 10.21
Number format(11.205) : 11.21
Number format(15.205) : 15.21

Number format(16.205) : 16.20
Number format(19.205) : 19.20
Number format(20.205) : 20.20

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@crh3675Apr 01.2005 — I could have sworn the function was:
[code=php]
print number_format(123.205,2);
[/code]


Where you specify the decimal at the second position
Copy linkTweet thisAlerts:
@NogDogApr 01.2005 — My guess is that you're running into the vagaries of floating point arithmetic, which is notorious for having essentially unpredictable variances at the least significant digit level. If the underlying code for the number_format() function is doing any sort of arithmetic functions with the input number to determine how to round it, you're likely to run into this sort of thing.
Copy linkTweet thisAlerts:
@web101authorApr 03.2005 — Yes crh365, well.. I basically cut paste the output.. Makes sense to show the code. Ok.. this was my code...

<?php

echo "<br>Number format(10.205) : ".number_format(10.205,2);

echo "<br>Number format(11.205) : ".number_format(11.205,2);

echo "<br>Number format(15.205) : ".number_format(15.205,2);

echo "<br>Number format(16.205) : ".number_format(16.205,2);

echo "<br>Number format(19.205) : ".number_format(19.205,2);

echo "<br>Number format(20.205) : ".number_format(20.205,2);

?>

So, this wierd rounding can't be explained... and is just a wrong behaviour by PHP ?
Copy linkTweet thisAlerts:
@NogDogApr 03.2005 — It's not exactly "wrong", but it is inconsistent. The problem is that when a number like "10.205" is operated on in floating point arithmetic, you tend to end up with results in memory like 10.2050000000001 or 10.2049999999999.

Even number_format() and sprintf() don't give exact same results. This code...
[code=php]
$test = array(10.205, 11.205, 12.205, 13.205, 14.205, 15.205, 16.205, 17.205,
18.205, 19.205, 20.205, 21.205, 29.205);
foreach($test as $value)
{
echo sprintf("Test value = %s, number_format = %s, sprintf value = %.2fn",
$value, number_format($value,2), $value);
}
[/code]

...generates this output...
<i>
</i>Test value = 10.205, number_format = 10.21, sprintf value = 10.21
Test value = 11.205, number_format = 11.21, sprintf value = 11.21
Test value = 12.205, number_format = 12.21, sprintf value = 12.21
Test value = 13.205, number_format = 13.21, sprintf value = 13.21
Test value = 14.205, number_format = 14.21, sprintf value = 14.21
Test value = 15.205, number_format = 15.21, sprintf value = 15.21
Test value = 16.205, number_format = 16.20, sprintf value = 16.20
Test value = 17.205, number_format = 17.20, sprintf value = 17.20
Test value = 18.205, number_format = 18.20, sprintf value = 18.20
Test value = 19.205, number_format = 19.20, sprintf value = 19.20
Test value = 20.205, number_format = 20.20, sprintf value = 20.20
Test value = 21.205, number_format = 21.21, sprintf value = 21.20
Test value = 29.205, number_format = 29.21, sprintf value = 29.20
×

Success!

Help @web101 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...