如何在 Python 中用逗号和指定的精度数字格式化数字 [英] How to format a number with comma and specified precision digits in Python

查看:64
本文介绍了如何在 Python 中用逗号和指定的精度数字格式化数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是针对 Python 2.6,这就是我们在生产中所拥有的.

我要求用逗号格式化数字(如 1234567.0987 或 1234567.0),并指定小数点后的位数.因此,如果精度为 3,则 1234567.0987 可能看起来像 1,234,567.099.

我尝试使用 Locale,正如许多问题的答案所建议的那样,问题是结果是小数点后两位,这不符合我的要求.

我在其他地方尝试过搜索,但没有找到任何解决方案,最后我自己创建了一个方法:

def format_float(value, precision = 4):formatString = "%0."+ str(精度) + "f"str_val = formatString % 值第一,第二 = str_val.split('.')第一 = 整数(第一)组 = []为真:结果,mod = first/1000,first % 1000group.append(str(mod))如果结果 == 0:休息第一 = 结果group.reverse()返回 '​​,'.join(group) + '.'+ 第二

我尝试运行一些测试来测试该方法并且它工作正常:

#默认4位精度断言 format_float(1234567890.0876543) == '1,234,567,890.0877'断言 format_float(1.2) == '1.2000'断言 format_float(1234) == '1,234.0000'断言 format_float(0) == '0.0000'# 3 位精度断言 format_float(1234567890.0876543, precision=3) == '1,234,567,890.088'断言 format_float(0, precision=3) == '0.000'

作为 Python 新手,我的问题是这是否是一个可以接受的解决方案.由于这种格式必须在一个紧凑的 for 循环中多次完成,如果有人能指出更好的解决方案,我将不胜感激.

谢谢大家

解决方案

我认为您对语言环境模块的了解不够深入.locale.format() 是您想要的,但请确保您先设置语言环境,否则根本无法进行分组.

<预><代码>>>>locale.setlocale(locale.LC_ALL, '')'en_US.UTF-8'>>>locale.format("%.4f", 12345.678912, grouping=True)'12,345.6789'

The question is for Python 2.6, that is what we have in production.

I have this requirement for formatting a number (like 1234567.0987 or 1234567.0) with comma, and specified number of digits after decimal points. So, it if precision is three, 1234567.0987 may look like 1,234,567.099.

I tried using Locale, as suggested by answers to many questions, the problem is that results in two digits after decimal, which is not acceptable for my requirement.

I tried searching in other places, but did not find any solution, and finally I created a method by my own:

def format_float(value, precision = 4):
    formatString = "%0." + str(precision) + "f"    
    str_val =  formatString % value
    first, second = str_val.split('.')
    first = int(first)
    group = []
    while True:
        result, mod = first / 1000, first % 1000
        group.append(str(mod))
        if result == 0:
            break
        first = result
    group.reverse() 
    return ','.join(group) + '.' + second

I tried to run some tests to test out the method and it works fine:

# default 4 digits precision
assert format_float(1234567890.0876543) == '1,234,567,890.0877'
assert format_float(1.2) == '1.2000'
assert format_float(1234) == '1,234.0000'
assert format_float(0) == '0.0000'

# 3 digits precision
assert format_float(1234567890.0876543, precision=3) == '1,234,567,890.088'
assert format_float(0, precision=3) == '0.000'

Being new to Python, my question is whether this is an acceptable solution. As this formatting has to be done many times in a tight for-loop, I would appreciate if someone can point to a better solution.

Thanks and Regards to All

解决方案

I don't think you looked deep enough into the locale module. locale.format() is what you want, but make sure you set a locale first or you won't get grouping at all.

>>> locale.setlocale(locale.LC_ALL, '')
'en_US.UTF-8'
>>> locale.format("%.4f", 12345.678912, grouping=True)
'12,345.6789'

这篇关于如何在 Python 中用逗号和指定的精度数字格式化数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆