PHP dropping decimals without rounding up

前端 未结 13 2305
独厮守ぢ
独厮守ぢ 2020-12-09 15:09

I want to drop off decimals without rounding up. For example if I have 1.505, I want to drop last decimal and value should be 1.50. Is there such a function in PHP?

13条回答
  •  生来不讨喜
    2020-12-09 15:35

    You need floor() in this way:

    $rounded = floor($float*100)/100;
    

    Or you cast to integer:

    $rounded = 0.01 * (int)($float*100);
    

    This way it will not be rounding up.

提交回复
热议问题