Round to max thousand, hundred etc in PHP

前端 未结 5 554
庸人自扰
庸人自扰 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条回答
  •  一生所求
    2020-11-30 15:23

    Here is my 2 cents:

    $v = 11;
    
    if(strlen($v)<4) { 
        $v = str_pad((int)(substr($v, 0, 1)+1), strlen($v), 0, STR_PAD_RIGHT);
    } else {
        $v = substr($v, 0, -4) . str_pad((int)(substr($v, -4, -3)+1), 4, 0, STR_PAD_RIGHT);
    }
    

提交回复
热议问题