在数值中加入千位分隔符的方法

来源:互联网 发布:阿里云lnmp 编辑:程序博客网 时间:2024/04/25 09:58
<script type="text/javascript"><!--google_ad_client = "pub-4490194096475053";/* 内容页,300x250,第一屏 */google_ad_slot = "3685991503";google_ad_width = 300;google_ad_height = 250;// --></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>

为了方便阅读,我们通常会在一串长数字中加入“千位分隔符”,即将 1234567890.11 转换成 1,234,567,890.11 ,这样的任务交给正则表达式来处理再方便不过了,本文就介绍了这个问题的解决方法。


语法:
Code:

(?<=[0-9])(?=(?:[0-9]{3})+(?![0-9]))





使用范例:
PHP: <?php // 说明:在数值中加入千位分隔符的方法 // 整理:CodeBit.cn ( http://www.CodeBit.cn ) $num = "1234567890.11"; $num = preg_replace('/(?<=[0-9])(?=(?:[0-9]{3})+(?![0-9]))/', ',', $num); echo $num; ?>

http://www.corange.cn/archives/2008/10/2076.html

原创粉丝点击