我需要打印一个有两个小数点的数字,我相信我使用了这段代码int $decimals = 0
,但我不知道我需要在哪里添加它。
这是我的代码:
<?php
$tempPrice = str_replace(',',"", $price); //gets rid of ","
$tempPrice = substr($tempPrice,2); //removes currency from the front
$tempPrice = floatval($tempPrice); //converts to double from string
if($tempPrice > 1000)
{
echo '£' . round(($tempPrice*0.038), 2) . ' per month';
}
else
{
echo 'Lease to buy price not available on this product';
}
?>
感谢
您可以使用money_format()php函数。http://php.net/money_format
例如,在您的情况下
<?php
$tempPrice = str_replace(',',"", $price); //gets rid of ","
$tempPrice = substr($tempPrice,2); //removes currency from the front
$tempPrice = floatval($tempPrice); //converts to double from string
// set international format for the en_GB locale
setlocale(LC_MONETARY, 'en_GB');
if($tempPrice > 1000)
{
echo money_format('%i', $tempPrice ) . " per month";// output->"GBP 1,234.56 per month"
}
else
{
echo 'Lease to buy price not available on this product';
}
?>
此外,您还可以使用phpnumber_format()http://php.net/number_format关于$tempPrice
echo "£ ". number_format($tempPrice ) . " per month";
默认情况下,使用number_format()英语表示法。您可以设置自己的小数点数量、小数点设置符和千位分隔符
echo "£ ". number_format($tempPrice, 2, ".", "," ) . " per month"; // for $tempPrice=1203.52 output will be "£ 1,203.56 per month"