PHP - 在数字之间添加空格的正确方法? [英] PHP - Correct way to add space between numbers?

查看:44
本文介绍了PHP - 在数字之间添加空格的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你知道答案,这可能是一个简单的问题.

Probably a simple problem if you know the answer.

我有什么

没有空格的数字.

$str = 10000;
$str2 = 100000;

我想要的

从上面转换到下面...

Convert above to below...

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

自己的想法

我想到了一些 strpos, regexp, str_replace 但这真的是解决这个问题的方法吗?我会接受最正确、最简短的代码答案,而且速度不慢.

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.

推荐答案

使用 number_format 函数.使用 space 表示 thhoudsandsSparator 如下.

Use number_format function. With space for thoudsandsSparator as below.

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

键盘演示.

这篇关于PHP - 在数字之间添加空格的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆