28

I want to round a number and I need a proper integer because I want to use it as an array key. The first "solution" that comes to mind is:

$key = (int)round($number)

However, I am unsure if this will always work. As far as I know (int) just truncates any decimals and since round($number) returns a float with theoretically limited precision, is it possible that round($number) returns something like 7.999999... and then $key is 7 instead of 8?

If this problem actually exists (I don't know how to test for it), how can it be solved? Maybe:

$key = (int)(round($number) + 0.0000000000000000001) // number of zeros chosen arbitrarily

Is there a better solution than this?

9
  • 4
    Integers stored within floats are always accurate, up to around 2^51, which is much more than can be stored in an int anyway. You are worrying over nothing. Aug 3, 2016 at 14:22
  • @NiettheDarkAbsol Oops, I actually knew that (from Javascript) but didn't make the connection. You should make that an answer. BTW, in JS it's 2^53-1 iirc.
    – AndreKR
    Aug 3, 2016 at 14:23
  • When you use rounded numbers as key, you run in trouble if you have more then one 7.x numbers with the rounded result of 8.
    – u-nik
    Aug 3, 2016 at 14:25
  • @u-nik It's exactly my intention that 7.4 and 7.6 end up with the same key. :)
    – AndreKR
    Aug 3, 2016 at 14:27
  • There are three functions to handle rounding numbers: round, [ceil][] (round up), [floor][] (round down). I'm unsure which one you want to use. [ceil]: secure.php.net/ceil [floor]: secure.php.net/floor
    – Exagone313
    Aug 3, 2016 at 14:28

7 Answers 7

45

To round floats properly, you can use:

Those functions return float, but from Niet the Dark Absol comment: "Integers stored within floats are always accurate, up to around 2^51, which is much more than can be stored in an int anyway."

0
7

round(), without a precision set always rounds to the nearest whole number. By default, round rounds to zero decimal places.

So:

$int = 8.998988776636;
round($int) //Will always be 9

$int = 8.344473773737377474;
round($int) //will always be 8

So, if your goal is to use this as a key for an array, this should be fine.

You can, of course, use modes and precision to specify exactly how you want round() to behave. See this.

UPDATE

You might actually be more interested in intval:

echo intval(round(4.7)); //returns int 5
echo intval(round(4.3)); // returns int 4
3
  • Nope, intval(4.7) == 4, which is not what I want.
    – AndreKR
    Aug 3, 2016 at 14:35
  • Yea, with intval by itself. Combine it with round. I've updated my answer
    – Zac Brown
    Aug 3, 2016 at 14:38
  • That suffers from the same (non-)problem that I described.
    – AndreKR
    Aug 3, 2016 at 14:41
2

What about simply adding 1/2 before casting to an int?

eg:

$int = (int) ($float + 0.5);

This should give a predictable result.

0
1

Integers stored within floats are always accurate, up to around 253, which is much more than can be stored in an int anyway. I am worrying over nothing.

0

For My Case, I have to make whole number by float or decimal type number. By these way i solved my problem. Hope It works For You.

$value1 = "46.2";
$value2 = "46.8";


// If we print by round()
echo round( $value1 );    //return float 46.0
echo round( $value2 );    //return float 47.0


// To Get the integer value
echo intval(round( $value1 ));   // return int 46
echo intval(round( $value2 ));   // return int 47
1
  • 1
    Please comment if have any Issue with the code. will try to update for better understand. Sep 23, 2020 at 9:55
-1

My solution:

function money_round(float $val, int $precision = 0): float|int
{
    $pow = pow(10, $precision);

    $result = (float)(intval((string)($val * $pow)) / $pow);
    if (str_contains((string)$result, '.')) {
        return (float)(intval((string)($val * $pow)) / $pow);
    }
    else {
        return (int)(intval((string)($val * $pow)) / $pow);
    }
}
1
  • 2
    Welcome to StackOverflow! Please provide an explanation for your code.
    – Roman
    Aug 26, 2021 at 12:10
-3

Round to the nearest integer

$key = round($number, 0);

3
  • 2
    It returns a float.
    – AndreKR
    Aug 3, 2016 at 14:27
  • How about $key = intval(round($number, 0)) or $key = (int)round($number, 0) Aug 4, 2016 at 18:58
  • 1
    Did you even read my question? I specifically gave (int)round($number) as an example of what I thought is not enough.
    – AndreKR
    Aug 5, 2016 at 5:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.