我对如何一起使用 number_format 和 round 函数有疑问,这是因为我有一个脚本来导入我供应商的所有产品,但我需要对价格进行四舍五入,例如:
供应商价格:1854.81 美元 四舍五入的价格:1854.99 美元(这是我想要的格式) 我的脚本打印的价格:1,854.90 美元
我尝试了 3 个 PHP 变体来做到这一点:
变体#1:
$preciosinr = 1854.81;
echo number_format(round($preciosinr*4)/4,2); //Print 1,854.90
变体 #2
$preciosinr = 1854.81;
$pos = 3 - floor(log10($preciosinr));
echo number_format(round($preciosinr,$pos)-0.10,2); //Print 1,854.75
变体 #3
$preciosinr = 1854.81;
number_format($preciosinr,2);
number_format(round($preciosinr,1),2);
number_format(round($preciosinr,0),2);
echo number_format(round($preciosinr,0)-0.01,2); //Print 1,854.99
如您所见,所有变体都用“,”打印价格,而我需要没有这个的价格,因为我的系统检测到的价格不正确。
我在 php.net 中读到我需要使用以下 sintaxis,但我不知道如何与我的代码集成。
// english notation without thousands separator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57
你能帮帮我吗?
最佳答案
$price=number_format(round($preciosinr,0)-0.01,2,'.','');
echo $price;
这应该可行
关于php - 如何在没有千位分隔符和舍入函数的情况下使用 number_format,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17375425/