我遇到了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);
答案 0 :(得分:1)
1,050.01
不是有效号码。这是一个格式化的字符串。所以,当你试着把它当成一个数字时,事情就会破裂。
要将数字四舍五入到小数位,请尝试以下方法:
$val = floor($num*100)/100;