在PHP中格式化尾随0的简单问题


Simple issues formatting trailing 0 with PHP

我试图采取一个字符串,如"2.4"和增量过去2.9到2.10,2.11等。请协助,已经找了两天了!这是我最后的尝试了。

    $update =$note;// (float) $note; 
     for($x=1;$x<sizeof($index);$x++){
        if(preg_match("/'.9/s", $update)){
            $u = explode(".", $update);
            $update = number_format($u[0]+".10", 2);
        }else{
            $u = explode(".", $update);
            if(strlen($u[1])==2){
                $add + $u[2]+="1";
                $update = "{$u[0]}"."."."{$u[1]}{$add}";
            }else{
                $update += ".1";
            }
        }
        echo $update."'r";
     }
}

试试这个:

$string = "2.42";
$nums = explode(".", $string);
$whole = $nums[0];
$decimals = intval($nums[1]);
$add_times = 10;
for ($i = 0; $i < $add_times; $i++) {
    $decimals += 1;
    $final_num = floatval($whole . "." . $decimals);
    var_dump($final_num);
}