Home  >  Article  >  php教程  >  php 带逗号千位符数字的处理方法

php 带逗号千位符数字的处理方法

WBOY
WBOYOriginal
2016-06-06 20:39:42972browse

php 带逗号千位符数字的处理方法,需要的朋友可以参考下。

通常用number_format(); 来格式化数字,默认情况千位符是用逗号间隔的,比如:
代码如下:
echo number_format("10000.01231", 2);
//取小数点后2位,输出的结果为:10,000.01 千位默认是用逗号间隔。

如果我们后台验证从客户端获取来的这种格式的数字就有点费劲了。

老修通常用 filter_input(INPUT_POST,"price",FILTER_VALIDATE_FLOAT) 来验证价格,如果是带逗号的千位符就会获取不到。

所以在确认输入是数字的时候就要先过滤掉所有的千位符,然后再验证。

其实很少有人会带千位符输入数字的,除非是复制的或者是把原来输出的值再提交。

在输出的时候我们就可以尽量少用带有千位符的数字格式, 输出可以这样写:
代码如下:
format_number("10000.01231",2,".","");
//这样输出的就是10000.01
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn