为了方便阅读,我们通常会在一串长数字中加入“千位分隔符”,即将 1234567890.11 转换成 1,234,567,890.11 ,这样的任务交给正则表达式来处理再方便不过了,本文就介绍了这个问题的解决方法。
语法:
(?<=[0-9])(?=(?:[0-9]{3})+(?![0-9]))
使用范例:
<?php // 说明:在数值中加入千位分隔符的方法 // 整理:CodeBit.cn ( http://www.CodeBit.cn ) $num = "1234567890.11"; $num = preg_replace('/(?<=[0-9])(?=(?:[0-9]{3})+(?![0-9]))/', ',', $num); echo $num; ?>
似乎可以使用number_formate可以完成类似的功能吧。:)
呵呵,当然了,假如只有数字的话,倒是可以用 number_format, 不过,正则的优势在于匹配,像 “$str = ‘今年营业额是1234567890.11亿元RMB';” 这样的字符串,就要靠本文介绍的方法了。
有需要这么复杂吗
使用 number_format($number);
很简单,
参考手栅
number_format
Format a number with grouped thousands (PHP 3, PHP 4, PHP 5)
string number_format ( float number [, int decimals [, string dec_point, string thousands_sep]] )
直接用 num_format 不更好吗?.
number_format 只能用于单纯的数字转换,而本文介绍的方法则可以处理字符串和数字混合的情况。
我觉得直接用number_formate
就可以解决啊
因为千 只存在与数字