20

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?

Pablo Camara
  • 622
  • 1
  • 5
  • 14

8 Answers8

23

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!

Pablo Camara
  • 622
  • 1
  • 5
  • 14
  • `ini_set("precision", 12);` should also work, editing php.ini would make it easier –  Oct 19 '15 at 23:03
  • 1
    This just seems like normal [floating point inaccuracy](http://stackoverflow.com/questions/3726721/php-math-precision) to me... – Matt Gibson Oct 19 '15 at 23:06
  • Alright, thanks for helping, next time I'll know what to do, got it fixed (Y) – Pablo Camara Oct 19 '15 at 23:06
  • 3
    Note that number_format() will add commas to large numbers. That may or may not be useful output, depending on your circumstances. – Lorien Brune Feb 23 '19 at 16:20
9

Just join empty string before round function like

$val = "" . round(9.597466245, 2);
Amit Kumar
  • 89
  • 1
  • 3
8

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

Joyie
  • 111
  • 1
  • 4
3

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
Julien
  • 1,765
  • 20
  • 26
2

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.

Justin Levene
  • 1,630
  • 19
  • 17
1

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.

0

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);
linktoahref
  • 7,812
  • 3
  • 29
  • 51
Bjorn
  • 11
  • 1
-1

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));
}
John Langford
  • 1,272
  • 2
  • 12
  • 15