2007年11月23日 星期五 14:32
1234567890987654321.123456789 如何格式化成为:1,234,567,890,987,654,321.123457 的字符串? -- Best Regards, No.23 ---- My Blog: http://blog.chinaunix.net/u1/42287/
2007年11月23日 星期五 14:53
On Nov 23, 2007 2:32 PM, 23号 <no.0023在gmail.com> wrote: > 1234567890987654321.123456789 > 如何格式化成为:1,234,567,890,987,654,321.123457 的字符串? > 这样行不: def f(num): n = num.split('.') if len(n[0]) <= 3: return num n[0] = f(n[0][:-3]) + ',' + n[0][-3:] return '.'.join(n) def main(): test = [('123456', '123,456'), ('123', '123'), ('1', '1'), ('1.123456', '1.123456'), ('1234567890', '1,234,567,890'), ('1234567890.1234567890', '1,234,567,890.1234567890')] for a,b in test: print '%s --> %s' % (a, f(a)) assert f(a) == b main() -- Best Regards, Leo Jay
2007年11月23日 星期五 14:59
django×Ô´øÁËÒ»¸ö´¦ÀíÕâ¸öµÄintcomma·½·¨ -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20071123/c086f31f/attachment.html
2007年11月23日 星期五 14:59
def dollar(f): if f < 0: # pull the sign out now so I know where to stop sign = "-" # when going bacwards f = -f else: sign = "" s = "%.2f" % f # -6 is the thousand's place -- 987654.21 # -3 goes back 1000 at a time # stop at 0 instead of -1 so "999.99" doesn't lead with a ",". for i in range(len(s)-6, 0, -3): # -6 is the first thousands place s = s[:i] + "," + s[i:] # Go back 1000 at a time return sign + s >>>* for i in (0, .9, 1, 10, 99.99, 100.00, 100.0, 999.99, 1000., * 1000.00, 1000000000L, -0.01, -0.9, -1, -99.9, -100, -999.99, -1000.00, -123456789): ... print i, dollar(i) ... 0 0.00 0.9 0.90 1 1.00 10 10.00 99.99 99.99 100.0 100.00 100.0 100.00 999.99 999.99 1000.0 1,000.00 1000.0 1,000.00 1000000000L 1,000,000,000.00 -0.01 -0.01 -0.9 -0.90 -1 -1.00 -99.9 -99.90 -100 -100.00 -999.99 -999.99 -1000.0 -1,000.00 -123456789 -123,456,789.00 On 11/23/07, Leo Jay <python.leojay在gmail.com> wrote: > > On Nov 23, 2007 2:32 PM, 23ºÅ <no.0023在gmail.com> wrote: > > 1234567890987654321.123456789 > > ÈçºÎ¸ñʽ»¯³ÉΪ£º1,234,567,890,987,654,321.123457 µÄ×Ö·û´®? > > > > > ÕâÑùÐв»£º > > def f(num): > n = num.split('.') > if len(n[0]) <= 3: > return num > > n[0] = f(n[0][:-3]) + ',' + n[0][-3:] > return '.'.join(n) > > def main(): > test = [('123456', '123,456'), > ('123', '123'), > ('1', '1'), > ('1.123456', '1.123456'), > ('1234567890', '1,234,567,890'), > ('1234567890.1234567890', '1,234,567,890.1234567890')] > > > for a,b in test: > print '%s --> %s' % (a, f(a)) > assert f(a) == b > > main() > > > -- > Best Regards, > Leo Jay > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20071123/3df0c964/attachment.htm
2007年11月23日 星期五 15:00
gooµÄ¡£ On 11/23/07, @@ <askfor在gmail.com> wrote: > > def dollar(f): > if f < 0: # pull the sign out now so I know where to stop > sign = "-" # when going bacwards > f = -f > else: > sign = "" > > s = "%.2f" % f > # -6 is the thousand's place -- 987654.21 > # -3 goes back 1000 at a time > # stop at 0 instead of -1 so "999.99" doesn't lead with a ",". > for i in range(len(s)-6, 0, -3): # -6 is the first thousands place > s = s[:i] + "," + s[i:] # Go back 1000 at a time > return sign + s > > >>>* for i in (0, .9, 1, 10, 99.99, 100.00, 100.0, 999.99, 1000., > * 1000.00, 1000000000L, -0.01, -0.9 , -1, -99.9, -100, -999.99, -1000.00, > -123456789): > ... print i, dollar(i) > ... > 0 0.00 > 0.9 0.90 > 1 1.00 > 10 10.00 > 99.99 99.99 > 100.0 100.00 > 100.0 100.00 > 999.99 999.99 > 1000.0 1,000.00 > 1000.0 1,000.00 > 1000000000L 1,000,000,000.00 > -0.01 -0.01 > -0.9 -0.90 > -1 -1.00 > -99.9 -99.90 > -100 -100.00 > -999.99 -999.99 > -1000.0 -1,000.00 > -123456789 -123,456,789.00 > > > On 11/23/07, Leo Jay <python.leojay在gmail.com> wrote: > > > > On Nov 23, 2007 2:32 PM, 23ºÅ <no.0023在gmail.com> wrote: > > > 1234567890987654321.123456789 > > > ÈçºÎ¸ñʽ»¯³ÉΪ£º1,234,567,890,987,654,321.123457 µÄ×Ö·û´®? > > > > > > > > > ÕâÑùÐв»£º > > > > def f(num): > > n = num.split('.') > > if len(n[0]) <= 3: > > return num > > > > n[0] = f(n[0][:-3]) + ',' + n[0][-3:] > > return '.'.join(n) > > > > def main(): > > test = [('123456', '123,456'), > > ('123', '123'), > > ('1', '1'), > > ('1.123456', '1.123456'), > > ('1234567890', '1,234,567,890'), > > ('1234567890.1234567890 ', '1,234,567,890.1234567890')] > > > > > > for a,b in test: > > print '%s --> %s' % (a, f(a)) > > assert f(a) == b > > > > main() > > > > > > -- > > Best Regards, > > Leo Jay > > _______________________________________________ > > python-chinese > > Post: send python-chinese在lists.python.cn > > Subscribe: send subscribe to python-chinese-request在lists.python.cn > > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20071123/bdcfa60d/attachment.html
2007年11月23日 星期五 15:03
´íÁË£¬ÄǸöÊÇÕûÊýµÄ -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20071123/e48a78ba/attachment-0001.htm
2007年11月23日 星期五 15:04
>>> locale.setlocale(locale.LC_ALL,"en") 'English_United States.1252' >>> locale.format('%.2f', 5555555555555.5555, 3) '5,555,555,555,555.56' >>> On 11/23/07, @@ <askfor在gmail.com> wrote: > > gooµÄ¡£ > > On 11/23/07, @@ <askfor在gmail.com> wrote: > > > > def dollar(f): > > if f < 0: # pull the sign out now so I know where to stop > > sign = "-" # when going bacwards > > f = -f > > else: > > sign = "" > > > > s = "%.2f" % f > > # -6 is the thousand's place -- 987654.21 > > # -3 goes back 1000 at a time > > # stop at 0 instead of -1 so "999.99 " doesn't lead with a ",". > > for i in range(len(s)-6, 0, -3): # -6 is the first thousands place > > s = s[:i] + "," + s[i:] # Go back 1000 at a time > > return sign + s > > > > >>>* for i in (0, .9, 1, 10, 99.99, 100.00, 100.0, 999.99, 1000., > > * 1000.00, 1000000000L, -0.01, -0.9 , -1, -99.9, -100, -999.99, - > > 1000.00, > > -123456789): > > ... print i, dollar(i) > > ... > > 0 0.00 > > 0.9 0.90 > > 1 1.00 > > 10 10.00 > > 99.99 99.99 > > 100.0 100.00 > > 100.0 100.00 > > 999.99 999.99 > > 1000.0 1,000.00 > > 1000.0 1,000.00 > > 1000000000L 1,000,000,000.00 > > -0.01 -0.01 > > -0.9 -0.90 > > -1 -1.00 > > -99.9 -99.90 > > -100 -100.00 > > -999.99 -999.99 > > -1000.0 -1,000.00 > > -123456789 -123,456,789.00 > > > > > > On 11/23/07, Leo Jay <python.leojay在gmail.com > wrote: > > > > > > On Nov 23, 2007 2:32 PM, 23ºÅ < no.0023在gmail.com> wrote: > > > > 1234567890987654321.123456789 > > > > ÈçºÎ¸ñʽ»¯³ÉΪ£º1,234,567,890,987,654,321.123457 µÄ×Ö·û´®? > > > > > > > > > > > > > ÕâÑùÐв»£º > > > > > > def f(num): > > > n = num.split('.') > > > if len(n[0]) <= 3: > > > return num > > > > > > n[0] = f(n[0][:-3]) + ',' + n[0][-3:] > > > return '.'.join(n) > > > > > > def main(): > > > test = [('123456', '123,456'), > > > ('123', '123'), > > > ('1', '1'), > > > ('1.123456', '1.123456'), > > > ('1234567890', '1,234,567,890'), > > > ('1234567890.1234567890 ', '1,234,567,890.1234567890')] > > > > > > > > > for a,b in test: > > > print '%s --> %s' % (a, f(a)) > > > assert f(a) == b > > > > > > main() > > > > > > > > > -- > > > Best Regards, > > > Leo Jay > > > _______________________________________________ > > > python-chinese > > > Post: send python-chinese在lists.python.cn > > > Subscribe: send subscribe to python-chinese-request在lists.python.cn > > > Unsubscribe: send unsubscribe to > > > python-chinese-request在lists.python.cn > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > > -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20071123/8d5e7dc0/attachment-0001.html
2007年11月23日 星期五 15:30
On Nov 23, 2007 2:32 PM, 23号 <no.0023在gmail.com> wrote: > 1234567890987654321.123456789 > 如何格式化成为:1,234,567,890,987,654,321.123457 的字符串? 下面是我自己用的代码: import re def integer(i): return FormatWithCommas("%d", i) def percent(f): return FormatWithCommas("%.02f%%", f) def currency(f): return FormatWithCommas("%.02f", f) re_digits_nondigits = re.compile(r'\d+|\D+') def FormatWithCommas(format, value): """ >>> FormatWithCommas('%.4f', .1234) '0.1234' >>> FormatWithCommas('%i', 100) '100' >>> FormatWithCommas('$%.4f', -1234567.5678) '$-1,234,567.5678' """ def _commafy(s): r = [] for i, c in enumerate(reversed(s)): if i and (not (i % 3)): r.insert(0, ',') r.insert(0, c) return ''.join(r) parts = re_digits_nondigits.findall(format % (value,)) for i in xrange(len(parts)): s = parts[i] if s.isdigit(): parts[i] = _commafy(s) break return ''.join(parts) 针对你的需要,首先这个浮点数太大,python无法保存足够的精度,所以只能 把它当做字符串处理。另外小数点后精度为6位,这部分需要单独处理。最后 大致应该是这样: >>> n = '1234567890987654321.123456789' >>> round, decimal = s.split('.') >>> FormatWithCommas('%s', round) #整数部分 '1,234,567,890,987,654,321' >>> ('%.6f'%float('.'+decimal))[1:] #小数部分 '.123457' >>> FormatWithCommas('%s', round)+('%.6f'%float('.'+decimal))[1:] #合起来得到结果 '1,234,567,890,987,654,321.123457'
2007年11月23日 星期五 15:46
On Nov 23, 2007 3:04 PM, @@ <askfor在gmail.com> wrote: > >>> locale.setlocale(locale.LC_ALL,"en") > 'English_United States.1252' > >>> locale.format('%.2f', 5555555555555.5555, 3) > '5,555,555,555,555.56' 这确实是一个可行的方法,但一个大问题是,代码运行的环境可能是对locale 很敏感的,比如web系统,来回切换locale有可能带来莫名其妙的问题。所以 有时只能舍近求远,自己写处理函数了。 另外,网上有很多人贡献了自己的代码,搜索一下就能找到好东西。我用的 FormatWithCommas就是取自ASPN,作者是Manuel Garcia,对我很合用 :)
2007年11月23日 星期五 15:54
import re def commify(ns): while True: (ns, count) = re.subn(r'^([-+]?\d+)(\d{3})', r'\1,\2', ns) if count == 0: break return ns bbs的答案。 On 11/23/07, Xie Yanbo <xieyanbo在gmail.com> wrote: > On Nov 23, 2007 3:04 PM, @@ <askfor在gmail.com> wrote: > > >>> locale.setlocale(locale.LC_ALL,"en") > > 'English_United States.1252' > > >>> locale.format('%.2f', 5555555555555.5555, 3) > > '5,555,555,555,555.56' > > 这确实是一个可行的方法,但一个大问题是,代码运行的环境可能是对locale > 很敏感的,比如web系统,来回切换locale有可能带来莫名其妙的问题。所以 > 有时只能舍近求远,自己写处理函数了。 > > 另外,网上有很多人贡献了自己的代码,搜索一下就能找到好东西。我用的 > FormatWithCommas就是取自ASPN,作者是Manuel Garcia,对我很合用 :) > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese -- Best Regards, No.23 ---- http://blog.chinaunix.net/u1/42287
2007年11月23日 星期五 16:03
On Nov 23, 2007 3:54 PM, 23号 <no.0023在gmail.com> wrote: > import re > > def commify(ns): > while True: > (ns, count) = re.subn(r'^([-+]?\d+)(\d{3})', r'\1,\2', ns) > if count == 0: break > return ns > > bbs的答案。 > 思路很有意思,不过就是性能太低了。 -- I like python! UliPad <>: http://code.google.com/p/ulipad/ meide < >: http://code.google.com/p/meide/ My Blog: http://www.donews.net/limodou
2007年11月23日 星期五 16:08
ÕýÔòÌ«Ç¿ÁË -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20071123/a6e08a2c/attachment.html
2007年11月23日 星期五 16:33
ÄãÊÇ˵Õâ¸öÕýÔòдµÄЧÂʵͣ¬»¹ÊDZ¾ÉíÕýÔòЧÂʾ͵ͣ¿ On Nov 23, 2007 4:03 PM, limodou <limodou在gmail.com> wrote: > On Nov 23, 2007 3:54 PM, 23ºÅ <no.0023在gmail.com> wrote: > > import re > > > > def commify(ns): > > while True: > > (ns, count) = re.subn(r'^([-+]?\d+)(\d{3})', r'\1,\2', ns) > > if count == 0: break > > return ns > > > > bbsµÄ´ð°¸¡£ > > > ˼·ºÜÓÐÒâ˼£¬²»¹ý¾ÍÊÇÐÔÄÜÌ«µÍÁË¡£ > > -- > I like python! > UliPad <>: http://code.google.com/p/ulipad/ > meide <>: http://code.google.com/p/meide/ > My Blog: http://www.donews.net/limodou > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20071123/9b275008/attachment.html
2007年11月23日 星期五 16:44
¸Õ²ÅÐÞ¸ÄÁËϲÎÊý£¬»úÆ÷¶¼¿ì¶¯²»ÁËÁË£¬Õâ¸öÕýÔò²»¿¿Æ× -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20071123/caa12a8f/attachment.htm
2007年11月23日 星期五 17:37
试了一下,还可以。 On 11/23/07, haur <hekun06在gmail.com> wrote: > 刚才修改了下参数,机器都快动不了了,这个正则不靠谱 > > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to > python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to > python-chinese-request在lists.python.cn > Detail Info: > http://python.cn/mailman/listinfo/python-chinese > -- Best Regards, No.23 ---- http://blog.chinaunix.net/u1/42287
Zeuux © 2025
京ICP备05028076号