[英]issue with php number_format()
我在使用number_format時遇到問題。 當$ val的值超過1,000時,$ update只會將值設置為1。如果值小於1,00,則會設置正確的值。
pmV為DECIMAL,7.2。
我敢肯定,我盯着這個看了太久了,並且錯過了一些東西。 我究竟做錯了什么? 請教育我! ;)
// Set variables for received data
$id = strval($_GET['id']); // value is 1
$k = strval($_GET['k']); // value is 1
$dwt = strval($_GET['dwt']); // value is 25
$spot = "." . strval($_GET['spot']); // value is .70
//Query the database based on the variables received and 'echo' the results back
$sql="SELECT * FROM metals WHERE id = '".$id."'";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
if ($id == 1){ // If we are calculating Gold, then add Karat into the calculation
$num = ((($row['value']/20)*$k)*$dwt)*$spot; //$row['value']=1200.01
}
else { // If not, then don't
$num = (($row['value']/20)*$dwt)*$spot;
}
$val = number_format($num,2,'.',',');
echo $val; // Send the value back to page --> Sending correct value - 1,050.01
// Update the DB with the calculated PM amount
$update="UPDATE totals SET pmV = $val WHERE id='1'";
$result2 = mysqli_query($con,$update); // UPDATES value of pmV to '1' instead of 1,050.01
// Get the Diamond Value from the DB and Update the Total calculation
$select="SELECT dV FROM totals WHERE id='1'";
$result3 = mysqli_query($con,$select);
while($dv = mysqli_fetch_array($result3)) {
$val2 = $dv['dV']+$val;
$sql4 = "UPDATE totals SET total = $val2 WHERE id='1'";
$result4 = mysqli_query($con,$sql4);
};
};
mysqli_close($con);
1,050.01
不是有效數字。 這是一個格式化的字符串。 因此,當您嘗試將其視為數字時,事情就會中斷。
要將數字四舍五入為小數點位數,請嘗試以下操作:
$val = floor($num*100)/100;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.