前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >玩转千位分隔符输出

玩转千位分隔符输出

作者头像
用户1177713
发布2018-02-24 17:18:38
1.7K0
发布2018-02-24 17:18:38
举报
文章被收录于专栏:数据之美

1、Python

1.1 format方法:

2.7版本以上直接用format设置千分位分隔符

代码语言:javascript
复制
Python 2.7 (r27:82500, Nov 23 2010, 18:07:12)

[GCC 4.1.2 20070115 (prerelease) (SUSE Linux)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> format(1234567890,',')

'1,234,567,890'

>>> print 'The value is {:0,.2f}'.format(1234567.125)
The value is 1,234,567.12
>>> print 'The value is {:0,.2f}'.format(1234567.126)
The value is 1,234,567.13

1.2 正则实现:

代码语言:javascript
复制
import re

def strConv(s):  

    s =  str(s)

    while True:

        (s,count) = re.subn(r"(\d)(\d{3})((:?,\d\d\d)*)$",r"\1,\2\3",s)

        if count == 0 : break

    return s

print strConv(12345)

1.3 locale

代码语言:javascript
复制
def number_format(num, places=0):

    """Format a number according to locality and given places"""

    locale.setlocale(locale.LC_ALL, "")

    return locale.format("%.*f", (places, num), True)

>>> import locale

>>> number_format(12345678.123)

'12,345,678'

>>> number_format(12345678.123, 2)

'12,345,678.12'



>>> import locale

>>> a = {'size': 123456789, 'unit': 'bytes'}

>>> print(locale.format("%(size).2f", a, 1))

123456789.00

>>> locale.setlocale(locale.LC_ALL, '') # Set the locale for your system

'en_US.UTF-8'

>>> print(locale.format("%(size).2f", a, 1))

123,456,789.00

1.4 DIY

代码语言:javascript
复制
>>> s = "1234567890"

>>> s = s[::-1]

>>> a = [s[i:i+3] for i in range(0,len(s),3)]

>>> print (",".join(a))[::-1]

2、Perl

代码语言:javascript
复制
perl -e '$size = "1234567890";while($size =~ s/(\d)(\d{3})((:?,\d\d\d)*)$/$1,$2$3/){};print $size, "\n";'

1,234,567,890

3、Sed

代码语言:javascript
复制
echo 12345|sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'

12,345

4、Bash

代码语言:javascript
复制
printf "%'d\n" 12345

12,345

5、JavaScript

5.1 Number.prototype.toLocaleString()  方法

代码语言:javascript
复制
parseInt('123456789456.34').toLocaleString()

"123,456,789,456"

5.2 Intl object 

代码语言:javascript
复制
Intl.NumberFormat().format(1234.1235);

"1,234.124"

5.3 正则

代码语言:javascript
复制
function addCommas(n){

    var rx=  /(\d+)(\d{3})/;

    return String(n).replace(/^\d+/, function(w){

        while(rx.test(w)){

            w= w.replace(rx, '$1,$2');

        }

        return w;

    });

}

addCommas('123456789456.34');

"123,456,789,456.34"



'12345678.34'.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")

"12,345,678.34"

注:某些方法不支持小数部分或者小数部分四舍五入,请慎用。

6、Refer:

[1] shell、perl、python 千分位 逗号分隔符输出

http://wenzhang.baidu.com/page/view?key=4f73729cefd8af8c-1426633956

[2] How do I add a thousand seperator to a number in JavaScript? [duplicate]

http://stackoverflow.com/questions/9743038/how-do-i-add-a-thousand-seperator-to-a-number-in-javascript

[3] How to print a number with commas as thousands separators in JavaScript

http://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、Python
    • 1.1 format方法:
      • 1.2 正则实现:
        • 1.3 locale
          • 1.4 DIY
          • 2、Perl
          • 3、Sed
          • 4、Bash
          • 5、JavaScript
            • 5.1 Number.prototype.toLocaleString()  方法
              • 5.2 Intl object 
                • 5.3 正则
                • 6、Refer:
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档