Round to max thousand, hundred etc in PHP

前端 未结 5 536
庸人自扰
庸人自扰 2020-11-30 14:24

I have a pretty simple PHP question but I\'m not sure how to do that.

I want to round to the max hundred or thousand depending on the value returned by the database

5条回答
  •  萌比男神i
    2020-11-30 15:12

    Final implementation, inspired from Shaunkak's answer and SO's comment. Thanks for these bra..s. live demo

    = 1000) {
        echo ceil($val / 1000) * 1000;
      }
      else {
        $length = strlen(ceil($val));
        $times = str_pad('1', $length, "0");
        echo ceil($val / $times) * $times;
    }
    

提交回复
热议问题