将 Python Decimal 对象格式化为指定的精度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15076310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Format Python Decimal object to a specified precision
提问by Joshua Burns
I've spent countless hours researching, reading, testing, and ultimately confused and dismayed at Python's Decimal object's lack of the most fundamental concept: Formatting a Decimal's output to a string.
我花了无数个小时研究、阅读、测试,最终对 Python 的 Decimal 对象缺乏最基本的概念感到困惑和沮丧:将 Decimal 的输出格式化为字符串。
Let's assume we have some strings or Decimal objects with the following values:
假设我们有一些具有以下值的字符串或 Decimal 对象:
0.0008
11.1111
222.2222
3333.3333
1234.5678
The goal is to simply set the Decimal's precision to the second decimal place. Eg, 11.1111would be formatted as 11.11, and 1234.5678as 1234.57.
目标是简单地将 Decimal 的精度设置为小数点后第二位。例如,11.1111将被格式化为11.11,和1234.5678作为1234.57。
I envision code similar to the following:
我设想的代码类似于以下内容:
import decimal
decimals = [
decimal.Decimal('0.0008'),
decimal.Decimal('11.1111'),
decimal.Decimal('222.2222'),
decimal.Decimal('3333.3333'),
decimal.Decimal('1234.5678'),
]
for dec in decimals:
print dec.as_string(precision=2, rounding=ROUND_HALF_UP)
The resulting output would be:
结果输出将是:
0.00
11.11
222.22
3333.33
1234.57
Obviously we cannot make use of the Decimal's context's precision, because this takes into consideration the TOTAL number of digits, not just decimal precision.
显然我们不能使用十进制上下文的精度,因为这考虑了总位数,而不仅仅是十进制精度。
I'm also not interested in converting the Decimal to a float to output its value. The ENTIRE reason behind Decimal is to avoid storing and running calculations on floats.
我也对将 Decimal 转换为浮点数以输出其值不感兴趣。Decimal 背后的全部原因是避免在浮点数上存储和运行计算。
What other solutions are there? I understand there are many other similar questions on stack overflow, but none of them have I found to resolve the underlying issue I am inquiring of.
还有哪些其他解决方案?我知道关于堆栈溢出还有许多其他类似的问题,但我发现没有一个可以解决我所询问的潜在问题。
Thanks much!
非常感谢!
采纳答案by Martijn Pieters
Just use string formattingor the format()function:
只需使用字符串格式或format()函数:
>>> for dec in decimals:
... print format(dec, '7.2f')
...
0.00
11.11
222.22
3333.33
1234.57
decimal.Decimalsupports the same format specificationsas floats do, so you can use exponent, fixed point, general, number or percentage formatting as needed.
decimal.Decimal支持与浮点数相同的格式规范,因此您可以根据需要使用指数、定点、一般、数字或百分比格式。
This is the official and pythonic method of formatting decimals; the Decimalclass implements the .__format__()method to handle such formatting efficiently.
这是格式化小数的官方和pythonic方法;的Decimal类实现的.__format__()方法有效地处理这样的格式。
回答by gseattle
def d(_in, decimal_places = 3):
''' Convert number to Decimal and do rounding, for doing calculations
Examples:
46.18271 to 46.183 rounded up
46.18749 to 46.187 rounded down
117.34999999999999 to 117.350
_rescale is a private function, bad practice yet works for now.
'''
return Decimal(_in)._rescale(-decimal_places, 'ROUND_HALF_EVEN')
Edit: Again, _rescale() is not meant to be used by us regular bipeds, it works in Python 2.7, is not available in 3.4.
编辑:同样,_rescale() 不适合我们常规 Biped 使用,它在 Python 2.7 中工作,在 3.4 中不可用。

