What is the exact equivalent of JS: something.toFixed() in PHP?

前端 未结 7 1465
Happy的楠姐
Happy的楠姐 2020-12-30 06:36

If I have a.toFixed(3); in javascript (\'a\' being equal to 2.4232) what is the exact equivalent command in php to retrieve that? I searched for it but found n

相关标签:
7条回答
  • 2020-12-30 07:15

    Here is the translation:

    function toFixed($number, $dec_length){
        $pos=strpos($number.'', ".");
        if($pos>0){
            $int_str=substr($number,0,$pos);
            $dec_str=substr($number, $pos+1);
            if(strlen($dec_str)>$dec_length){
                return $int_str.($dec_length>0?'.':'').substr($dec_str, 0,$dec_length);
            }else{
                return $number;
            }
        }else{
            return $number;
        }
    }
    
    
    //toFixed(1234.678, 0) = 1234
    //toFixed(1234.678, 1) = 1234.6
    //toFixed(1234.678, 2) = 1234.67
    //toFixed(1234.678, 4) = 1234.678
    
    0 讨论(0)
提交回复
热议问题