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

前端 未结 7 1470
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条回答
  •  Happy的楠姐
    2020-12-30 07:01

    I found that sprintf and number_format both round the number, so i used this:

    $number = 2.4232;
    $decimals = 3;
    $expo = pow(10,$decimals);
    $number = intval($number*$expo)/$expo; //  = 2423/100
    

提交回复
热议问题