简体   繁体   English

PHP:number_format问题

[英]PHP: number_format issue

Iv taken over a very old php project that is still currently in use. iv接管了一个仍在使用中的非常老的php项目。 Its a custom "shopping" website. 它是一个自定义的“购物”网站。

When a product gets ordered it inserts the money value into the database as a string of integers: 订购产品时,它将货币值作为整数字符串插入数据库:

2000

which then gets outputted as follows: 然后输出如下:

R20,00 R20,00

Using this code 使用此代码

echo number_format($order['order_total'] / 100, 2, ",", "");

My problem is that if an order gets placed to the value of R 15 256.00 The record gets inserted as such: 我的问题是,如果将订单下达R 15 256.00的值,则将记录插入为:

15256

and gets outputted to the user as: 并以以下形式输出给用户:

R 152,56 R 152,56

But it should ultimately show like this 但是它最终应该像这样显示

R15, 256.00 R15,256.00

I do understand that the problem comes in by dividing the amount by 100 in the above query. 我确实知道问题是通过在上述查询中将金额除以100来解决的。 If i alter the query then the larger numbers appear fine but the smaller amounts such as R20,00 get shown as R2000,00 如果我更改查询,则较大的数字看起来很好,但较小的数量(例如R20,00)显示为R2000,00

Any help would be great. 任何帮助都会很棒。

You can you PHP's native function money_format() , which will format the number as a currency. 您可以使用PHP的本机函数money_format() ,该函数money_format()数字格式化为货币。 Using setlocale() you can set the appropriate locale. 使用setlocale()可以设置适当的语言环境。

setlocale(LC_MONETARY, 'en_IN'); 
echo money_format('%!i', 15256);

This will output: 这将输出:

15,256.00 15,256.00

Try This 尝试这个

echo number_format($order['order_total'] / 100, 2, ".", ",");

you can read more about number format here . 您可以在此处阅读有关数字格式的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM