我需要让这个打印件有一个带有两个小数点的数字,我相信我使用这段代码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';
}
?>
由于
答案 0 :(得分:0)
你可以使用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';
}
?>
另外你可以在$ tempPrice上使用php number_format()http://php.net/number_format
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"