简体   繁体   中英

Simple issues formatting trailing 0 with PHP

I am trying to take a string like "2.4" and increment past 2.9 to 2.10, 2.11 and so fowarth. Please assist, been two days of searching! This is my last desperate attempt.

    $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";
     }
}

Try this:

$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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM