我需要打印一个有两个小数点的数字,我相信我使用了这段代码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';
}
?>谢谢
发布于 2015-03-31 17:37:17
您可以使用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"https://stackoverflow.com/questions/29365201
复制相似问题