简体   繁体   中英

How do i strip unwanted characters from an amount in php?

I'm making a tool for school which determines how much weekly, monthly and daily you earn.

but the problem is that when i echo out the amounts. It gives me a bunch of other numbers which are not needed.

How do i remove them from my amounts?

When I input 52000 it gives me this.

Amount per week: $1000 Amount per day: $142.46575342466

The "per week" is correct and so is the per day amount, but why are there all those numbers on the end? i only need the first 2 after the decimal.

this is my code for php.

Sorry this isn't with the code tag things, i couldn't get them to work. it kept giving me error :(

<html>
<head>
<title>test</title>
</head>
<style type="text/css">
//style goes here.
</style>
<body>
<center>
<form action="" method="POST">
<input type="text" name="amount" placeholder="Amount you earn">
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit']))
{
if(!empty($_POST['amount']))
{
//хранить сумму в $amount
$amount=$_POST['amount'];
//добавить суммы
$weekly=$amount / 52;
$daily=$amount / 365;
//писать сумму
echo "Amount per week: <b>$" . $weekly . "</b>";
echo "<br>";
echo "Amount per day: <b>$". $daily . "</b>";
}
else
{
//сообщение, если вы не введете ничего
echo "<pre>Nothing entered, please enter an amount.";
}
}
?>
</body>
</html>

any help would be awesome :D

您应该使用此代码

echo "Amount per day: <b>$". number_format($daily/100, 2, ",", "") . "</b>";

"why are there all those numbers on the end?"

It is called math. To format the output you can use number_format()

echo '$' . number_format($daily, 2);

This will output:

$142.46

除了数字格式之外,您还可以使用round()函数:

echo "Amount per day: <b>$". round($daily, 2) . "</b>";

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