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