PHP round to integer

后端 未结 6 464
再見小時候
再見小時候 2021-01-07 22:54

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)roun         


        
6条回答
  •  醉酒成梦
    2021-01-07 23:52

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

    eg:

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

    This should give a predictable result.

提交回复
热议问题