4 回答

TA貢獻(xiàn)1795條經(jīng)驗(yàn) 獲得超7個(gè)贊
您正在比較浮點(diǎn)數(shù)。嘗試使用
while (bccomp($total_amount, $request_amount) === -1) {

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超1個(gè)贊
使用小數(shù)分?jǐn)?shù)作為浮點(diǎn)數(shù)進(jìn)行計(jì)算時(shí)會(huì)發(fā)生錯(cuò)誤。浮點(diǎn)數(shù)不能精確表示某些十進(jìn)制數(shù)。僅使用 BC 數(shù)學(xué)函數(shù)進(jìn)行此類計(jì)算。您也不需要循環(huán)。
$amount_perpoints = "622.91"; // AMOUNT OF MONEY PER POINTS
$request_amount = "3114.55"; //REQUESTED AMOUNT OF POINTS for result 5.0
//$request_amount = "3426.005"; //REQUESTED AMOUNT OF POINTS for result 5.5
$point_step = "0.50";
//calculation
$points = bcdiv($request_amount,$point_step,2);
$points = bcdiv($points,$amount_perpoints,0);
$points = bcmul($points,$point_step,1);
//output
var_dump($points); //string(3) "5.0"

TA貢獻(xiàn)1770條經(jīng)驗(yàn) 獲得超3個(gè)贊
<?php
$amount_perpoints = bcdiv(622.9106666666667,1,2); // AMOUNT OF MONEY PER POINTS
$request_amount = 3114.55; //REQUESTED AMOUNT OF POINTS
$points = 0; // THIS WILL CONTAIN THE TOTAL POINTS
$new_points=0;
$total_amount = 0; // THIS WILL INCREMENT ACCORDING TO THE PRODUCT OF THE CURRENT POINT AND AMOUNT PER POINTS
while($total_amount < $request_amount){
$points=$new_points;
$new_points = $new_points+0.50; //POINTS INCREMENTING BY 0.5
$total_amount = $new_points * $amount_perpoints;
}
echo $points;
?>

TA貢獻(xiàn)2051條經(jīng)驗(yàn) 獲得超10個(gè)贊
我認(rèn)為@jeff是對(duì)的:
$amount_perpoints = bcdiv(622.9106666666667,1,2); // AMOUNT OF MONEY PER POINTS
$request_amount = 3114.55; //REQUESTED AMOUNT OF POINTS
$points = 0; // THIS WILL CONTAIN THE TOTAL POINTS
$total_amount = 0; // THIS WILL INCREMENT ACCORDING TO THE PRODUCT OF THE CURRENT POINT AND AMOUNT PER POINTS
while($total_amount <= $request_amount){
$points = $points+0.50; //POINTS INCREMENTING BY 0.5
$total_amount = $points * $amount_perpoints;
}
echo $points;
由于你數(shù)到它達(dá)到期望的點(diǎn),它的靈魂是少或等于。它給出的輸出為 : 5.5
- 4 回答
- 0 關(guān)注
- 176 瀏覽
添加回答
舉報(bào)