PHP货币格式与空格

Cle*_*kod 10 php money-format

我想知道是否有办法将千位与空间角色分开.

例如:

$number = 21234.56;
setlocale(LC_ALL, 'pl_PL.utf8');
echo money_format('%i', $number);
Run Code Online (Sandbox Code Playgroud)

给我的是:

21.234,56 PLN

而且我要:

21 234,56 PLN

我不想制作str_replace.我相信有更好的方法.

krt*_*tek 19

你可以用number-format.例如 :

echo number_format($number, 2, ',', ' ');
Run Code Online (Sandbox Code Playgroud)

但是你必须自己装置.

否则,此评论中的money_format文档页面给出了一些代码,您可以修改改变千分一版本的功能.

你也可以这样做:

$locale = localeconv();
$text = money_format('%i', $number);
$text = str_replace($locale['mon_thousands_sep'], ' ', $text);
Run Code Online (Sandbox Code Playgroud)


lif*_*der 6

请参阅:http://php.net/manual/en/function.number-format.php

string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )

对于你的情况:

number_format($number, 2, ',', ' ');
Run Code Online (Sandbox Code Playgroud)