2017-07-30 7 views
0

私のウェブサイトには金額が表示されています。私は金額から引き算すると、ログアウトして自分のアカウントに戻すまで、最後のゼロを取り除きます。 10進数の後に常に2つの数字があることを確認し、存在しない場合はゼロを追加する方法はありますか?ここで 小数点に数字があることを確認するにはどうすればいいですか(末尾のゼロ)

は私の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エコーコードです:あなたのであれば

$cash_amount = $_SESSION['cash_amount']; 

:文書の冒頭で

<?php echo '$'.$cash_amount ?> 

私を助けることができた、私は非常に非常に感謝するだろう。みんなありがとう!

+0

(http://php.net/manual/en/function.sprintf.php)[ 'sprintf']お試しくださいまたは['str_pad'](http://php.net/manual/en/function.str-pad.php)を参照してください。 – Tigger

答えて

0

echo sprintf('%0.2f', $cash_amount); 

それとも、HTML/PHPで試してみてください。

... 
$<?php echo sprintf('%0.2f', $cash_amount) ?> 
... 
+0

完璧!本当にありがとう! – Patrick

関連する問題