Php 在哪里添加int$decimals=0?
我需要把这张打印出来的数字加上两个小数点,我相信我使用了这段代码int$decimals=0,但我不知道我需要在哪里添加它 以下是我的代码:Php 在哪里添加int$decimals=0?,php,number-formatting,Php,Number Formatting,我需要把这张打印出来的数字加上两个小数点,我相信我使用了这段代码int$decimals=0,但我不知道我需要在哪里添加它 以下是我的代码: <?php $tempPrice = str_replace(',',"", $price); //gets rid of "," $tempPrice = substr($tempPrice,2); //removes currency from the front $tempPrice = floatval($tempPrice)
<?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\u格式的php函数 比如你的情况
<?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';
}
?>
默认情况下,使用数字\格式的英文符号。您可以设置自己的小数点数量、小数点设置器和千位分隔符
echo "£ ". number_format($tempPrice, 2, ".", "," ) . " per month"; // for $tempPrice=1203.52 output will be "£ 1,203.56 per month"
嗨,彼得,谢谢你的帮助。第一个示例打印的是英镑,有没有办法将其更改为英镑?如果我更改了数字格式,则使用数字格式替换。轮到我了。数字格式?问题的第一部分在这里之前已经回答了。在第二部分。。。是,更改它,但检查数字\格式功能以将其设置为您的需要。
echo "£ ". number_format($tempPrice, 2, ".", "," ) . " per month"; // for $tempPrice=1203.52 output will be "£ 1,203.56 per month"