PHP货币格式在需要时添加小数和逗号

Pap*_*eau 0 php currency number-formatting money-format

如何使用php 将这个数字247936变成这个$ 2,479.36

我尝试了几个选项,例如:

setlocale(LC_MONETARY, 'en_US');
echo money_format('%i', '247936'); // OUTPUT: USD 247,936.00 
Run Code Online (Sandbox Code Playgroud)

我也尝试过这个

echo number_format(247936, 2);  // echos '247,936.00'
Run Code Online (Sandbox Code Playgroud)

我甚至试过这个,但它没有用

echo number_format(247936, 2,'.', ','); // echos 247,936.00
Run Code Online (Sandbox Code Playgroud)

Sev*_*kov 10

看起来你的变量是美分,所以除以100

echo number_format((247936 / 100), 2, '.', ',');
Run Code Online (Sandbox Code Playgroud)

输出是: 2,479.36