PHP - Correct way to add space between numbers?

点点圈 提交于 2020-01-24 05:42:19

问题


Probably a simple problem if you know the answer.

What I have

Numbers with no spaces.

$str = 10000;
$str2 = 100000;

What I want

Convert above to below...

$str = '10 000';
$str2 = '100 000';

Own thoughts

I thought of some strpos, regexp, str_replace but is it really the way to solve this? I will accept the most correct, short code answer with no lack of speed.


回答1:


Use number_format function. With space for thoudsandsSparator as below.

number_format($number, 0, '.', ' ')

Codepad Demo.




回答2:


Look no further than number_format.




回答3:


use number_format:

$formatted = number_format(
    $number, $numberOfDecimals, $decimalSeperator, $thousandsSeperator);


来源:https://stackoverflow.com/questions/15677094/php-correct-way-to-add-space-between-numbers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!