PHP money format add decimal and comma when needed

Papa De Beau picture Papa De Beau · Aug 8, 2018 · Viewed 7.1k times · Source

How do I turn this number 247936 into this $2,479.36 using php?

I tried several options such as:

setlocale(LC_MONETARY, 'en_US');
echo money_format('%i', '247936'); // OUTPUT: USD 247,936.00 

I also tried this

echo number_format(247936, 2);  // echos '247,936.00'

I even tried this but it didn't work as well

echo number_format(247936, 2,'.', ','); // echos 247,936.00

Answer

Seva Kalashnikov picture Seva Kalashnikov · Aug 8, 2018

It looks like your variable is in cents, so divide it by 100

echo number_format((247936 / 100), 2, '.', ',');

Output is: 2,479.36