PHP round to integer

后端 未结 6 461
再見小時候
再見小時候 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:40

    To round floats properly, you can use:

    • ceil($number): round up
    • round($number, 0): round to the nearest integer
    • floor($number): round down

    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."

提交回复
热议问题