简体   繁体   中英

How do I stop my database from rounding to 16 decimal places?

I'm trying to make a webpage, where when the user clicks a payment button, a set amount it subtracted from an existing value in the database table. Just to confirm: My page does do this. But the unexpected behaviour of my code is it subtracts the amount, but it doesn't round it. It rounds it to 16 decimal places and I only want 2. See the image below (the bottom row) to see what I mean.

I have tried using the number_format() and round() functions in PHP but I'm not sure why they don't work. Am I using them incorrectly?

数据库表中的意外行为

This is the full code below:

if(isset($_REQUEST['submit'])) {
  $SQL_query = "SELECT ROUND(SUM(Total), 2) AS sum FROM `order_details` WHERE `OrderID` = 95";
  $query_result = mysqli_query($connection, $SQL_query);

  while($row = mysqli_fetch_assoc($query_result)) {
  $total = $row['sum'];
  $number = floatval($total);
  }

  $SQL_Balance = "UPDATE Users SET Balance=(Balance-$number) WHERE UserID = 7";
  mysqli_query($connection, $SQL_Balance);


} 
 

To get a two decimal number you can use ROUND with the following query:

UPDATE Users SET Balance=(ROUND(Balance-$number, 2)) WHERE UserID = 7

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM