0
在我的网站上,有金额被回应。当我从金额中扣除时,它会删除尾部零,直到我注销并重新登录到我的帐户。有没有办法检查小数点后面总是有两个数字,如果没有,就添加一个零点?我怎样才能确保有一定数量的小数(尾随零)
这里是我的PHP减法代码:
<?php
session_start();
$servername = "localhost";
$username = "root";
$password = "patrick2002";
$dbname = "accounts";
$cash_amount = $_SESSION['cash_amount'];
// Create connection
$userid = $_SESSION['id'];
// You must enter the user's id here. /\
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Fetch the existing value of the cash_amount against that particular user here. You can use the SELECT cash_amount from users where userid = $userid
$_SESSION['cash_amount'] -= 0.05;
$newAmount = $cash_amount - 0.05;
$sql = "UPDATE users SET cash_amount = $newAmount WHERE id = $userid";
$result = $conn->query($sql);
if($result)
{
echo "5 cents has been subtracted!";
}
else
{
echo mysqli_error($conn);
}
$conn->close();
?>
这里是我的PHP回声代码:
<?php echo '$'.$cash_amount ?>
在文档的开头:
$cash_amount = $_SESSION['cash_amount'];
所以,如果你可以帮助我,我会非常感激。多谢你们!
尝试['sprintf'(http://php.net/manual/en/function.sprintf.php)或['str_pad'](http://php.net/manual/en/function.str-pad.php)。 – Tigger