2013-12-13 9 views
1

number_format에 문제가 있습니다. $ val 값이 1,000을 초과하면 $ update는 값 1 만 설정합니다. 값이 1,00보다 작 으면 올바른 값을 설정합니다.php number_format() 문제가 발생했습니다.

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

오히려'조인 한'UPDATE' 문에 최선을 다하고보다, PHP에서 계산을하고있는 이유가 있나요 합계'금속 '? – Barmar

+0

이것은 전당포 직원이 다이아몬드와 귀금속에 대해 어떤 가치를 부여하는지 계산할 수있는 내부 계산기입니다. 이 php 파일은 귀금속에 대해 양을 표시하고 DB에 해당 금액을 추가하고 다른 열의 총계를 다시 계산하여 다이아몬드가 폰에 관련되어 있으면 페이지가 누적 합계를 줄 수 있도록해야합니다. . 나는이 같은 목표를 성취 할 수있는 더 좋은 방법이 있다면 모든 귀입니다. – thelincster

답변

1

1,050.01은 유효한 숫자가 아닙니다. 형식이 지정된 문자열입니다. 그래서 숫자처럼 취급하려 할 때, 일이 깨어집니다.

소수 자릿수를 TEO하는 숫자를 반올림하려면 다음을 수행하십시오

$val = floor($num*100)/100; 
+0

그건 의미가 있습니다. 니트 감사합니다! – thelincster

관련 문제