简体   繁体   中英

PHP: number_format issue

Iv taken over a very old php project that is still currently in use. 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

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:

15256

and gets outputted to the user as:

R 152,56

But it should ultimately show like this

R15, 256.00

I do understand that the problem comes in by dividing the amount by 100 in the above query. If i alter the query then the larger numbers appear fine but the smaller amounts such as R20,00 get shown as R2000,00

Any help would be great.

You can you PHP's native function money_format() , which will format the number as a currency. Using setlocale() you can set the appropriate locale.

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

This will output:

15,256.00

Try This

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

you can read more about number format here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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