[英]issue with php number_format()
I'm having an issue with number_format. 我在使用number_format时遇到问题。 When $val is over 1,000 in value, the $update will only SET a value of 1. If it is less than 1,00 in value, it will SET the correct value.
当$ val的值超过1,000时,$ update只会将值设置为1。如果值小于1,00,则会设置正确的值。
pmV is DECIMAL, 7,2. pmV为DECIMAL,7.2。
I'm sure that I have just stared at this for too long and am missing something. 我敢肯定,我盯着这个看了太久了,并且错过了一些东西。 What am I doing wrong?
我究竟做错了什么? Please school me!
请教育我! ;)
;)
// 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
is not a valid number. 1,050.01
不是有效数字。 It's a formatted string. 这是一个格式化的字符串。 So when you try to treat it like a number, things break.
因此,当您尝试将其视为数字时,事情就会中断。
To round a number to teo decimal places, try this: 要将数字四舍五入为小数点位数,请尝试以下操作:
$val = floor($num*100)/100;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.