Pablo Camara
Pablo Camara

Reputation: 682

PHP Round() not working properly

I need help. I'm doing this:

round($myvar,2);

And this is the number I'm getting: 9.779999999999999€

With the other variables it works just fine, any idea how to fix this?

Upvotes: 23

Views: 27236

Answers (8)

Bjorn
Bjorn

Reputation: 11

I had the same problem and my workaround (in order to not change the ini-file and possibly break something else):

$rounded = bcdiv($number, 1, 2);

Upvotes: 0

Justin Levene
Justin Levene

Reputation: 1677

Just encase the result in a floatval

floatval(round($myvar,2));

If you use number_format, you must add the next two parameters otherwise larger numbers will go wrong:

number_format($myvar,2, '.', '');

This tells PHP to use "." as the decimal separator and no thousands separator.

Upvotes: 2

Amit Kumar
Amit Kumar

Reputation: 119

Just join empty string before round function like

$val = "" . round(9.597466245, 2);

Upvotes: 12

John Langford
John Langford

Reputation: 1435

Combining @PabloCamara's answer and the comment from @LorienBrune, this solution worked for me:

function roundFixed($number, $decimalPlaces)
{
    return str_replace(',', '', number_format($number, $decimalPlaces));
}

Upvotes: -1

Julien
Julien

Reputation: 1925

something (could be in some 3rd party code ?) set your ini setting precision to 16

$php -a
php > echo ini_get("precision");
14 // default 
php > echo round(9.7752,2);
9.78

php > echo ini_set("precision", 16);
14
php > echo round(9.7752,2);
9.779999999999999

Upvotes: 3

Joyie
Joyie

Reputation: 111

what problem I encounter is that round(18.203,2) = 18.2 ,then json_encode(...) = 18.199999999 . so I find the solution is json_encode(strval(round(18.203,2))) = 18.2 it works

Upvotes: 8

Mladen Lotar
Mladen Lotar

Reputation: 49

Although OP solved his issue, I saw this while searching for other (but similar) issue.

If you came here with a problem where PHP's round() function is misbehaving (i.e. round(1.4447, 2) will output 1.44 instead of 1.45) that's because round looks just 1 decimal position away from the needed precision, here's a small recursive helper function that solves the issue:

function roundFloat($float, $neededPrecision, $startAt = 7)
{
    if ($neededPrecision < $startAt) {
        $startAt--;
        $newFloat = round($float, $startAt);
        return roundFloat($newFloat, $neededPrecision, $startAt);
    }

    return $float;
}

Usage example:

roundFloat(1.4447, 2, 5); // Returns 1.45

Explanation:

  • In example above, it starts rounding from 5th decimal point (0) in this case since not stated otherwise.
  • You can call the function like roundFloat(1.4447, 2, 15) with same outcome for given float 1.4447.
  • $startAt just defines the starting point for the "rounding process" or the required precision.

Note: all programming languages have issues with writing some floats precisely (has to do with binary, google that for more info).

Hope it helps someone.

Upvotes: 0

Pablo Camara
Pablo Camara

Reputation: 682

I did this:

<?php echo round(9.7752,2);?>

And I got: 9.779999999999999

I believe it's something in php.ini as @MarkBaker said.. But, I fixed it by doing:

<?php echo number_format($myvar,2);

And I got exactly what I wanted. Thanks guys for the help!

Upvotes: 24

Related Questions