简体   繁体   English

如何阻止我的数据库四舍五入到小数点后 16 位?

[英]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.它将它四舍五入到小数点后 16 位,我只想要 2。请参见下图(底行)以了解我的意思。

I have tried using the number_format() and round() functions in PHP but I'm not sure why they don't work.我曾尝试在 PHP 中使用 number_format() 和 round() 函数,但我不确定它们为什么不起作用。 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:要获得两位十进制数,您可以将 ROUND 与以下查询一起使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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