【发布时间】:2022-04-16 14:00:40
【问题描述】:
我正在尝试使用 PHP 将数字格式化为
删除所有尾随零
为千位分隔符添加逗号
列出两个小数点,假设它们不是零
我试过这个,但它并没有完全达到我想要达到的效果:
$prices[$title]['reg_price'] = (float)number_format($membership->sell_price, 2, ".", "");
$prices[$title]['three_year_price'] = (float)number_format($membership->attributes[$aid]->options[$three_year_oid]->price, 2, ".", "");
我发现我可以通过将数字转换为浮点数来去除尾随零。但是,我发现我需要告诉 number_format 不要使用千位逗号分隔符,因为否则,将 1,500.00 转换为浮点数时,结果为 1。
因此,总而言之,我希望我的代码将 1500.00 更改为 1,500,将 150.00 更改为 150,将 19.99 更改为 19.99。我怎样才能做到这一点?
【问题讨论】:
标签: php