phpcurrencynumber-formattingmoney-format

PHP money format add decimal and comma when needed


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

Solution

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

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

    Output is: 2,479.36