如何格式化小数以始终显示 2 个小数位? [英] How can I format a decimal to always show 2 decimal places?

查看:46
本文介绍了如何格式化小数以始终显示 2 个小数位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示:

4949.00

和:

54.954.90

无论小数的长度或是否有小数位,我都想显示一个带有2个小数位的Decimal,我想以一种有效的方式来做.目的是显示货币价值.

例如,4898489.00

解决方案

我想您可能正在使用 Decimal() 来自 decimal 模块?(如果您需要小数点后精确到任意大数的两位数,您绝对应该这样做,这就是您的问题标题所暗示的......)

如果是这样,Decimal文档的 FAQ 部分有一个问题/答案对,可能对您有用:

<块引用>

问.在有两位小数的定点应用中,有些输入有很多位,需要四舍五入.其他人不应该有多余的数字,需要进行验证.应该使用哪些方法?

A.quantize() 方法四舍五入到固定的小数位数.如果设置了 Inexact 陷阱,它也可用于验证:

<预><代码>>>>TWOPLACES = Decimal(10) ** -2 # 与 Decimal('0.01') 相同>>># 轮到两个地方>>>十进制('3.214').量化(两个地方)十进制('3.21')>>># 验证一个数字不超过两位>>>Decimal('3.21').quantize(TWOPLACES, context=Context(traps=[Inexact]))十进制('3.21')>>>Decimal('3.214').quantize(TWOPLACES, context=Context(traps=[Inexact]))回溯(最近一次调用最后一次):...不准确:无

下一个问题是

<块引用>

问.一旦我有有效的两个地方输入,我如何在整个应用程序中保持这种不变性?

如果您需要答案(以及许多其他有用的信息),请参阅 文档的上述部分.此外,如果您将 Decimal 保持在小数点后两位精度(意味着将所有数字保留在小数点左侧和右侧两位所需的精度)没有更多......),然后将它们转换为带有 str 的字符串将可以正常工作:

str(Decimal('10'))# ->'10'str(十进制('10.00'))# ->'10.00'str(十进制('10.000'))# ->'10.000'

I want to display:

49 as 49.00

and:

54.9 as 54.90

Regardless of the length of the decimal or whether there are are any decimal places, I would like to display a Decimal with 2 decimal places, and I'd like to do it in an efficient way. The purpose is to display money values.

eg, 4898489.00

解决方案

I suppose you're probably using the Decimal() objects from the decimal module? (If you need exactly two digits of precision beyond the decimal point with arbitrarily large numbers, you definitely should be, and that's what your question's title suggests...)

If so, the Decimal FAQ section of the docs has a question/answer pair which may be useful for you:

Q. In a fixed-point application with two decimal places, some inputs have many places and need to be rounded. Others are not supposed to have excess digits and need to be validated. What methods should be used?

A. The quantize() method rounds to a fixed number of decimal places. If the Inexact trap is set, it is also useful for validation:

>>> TWOPLACES = Decimal(10) ** -2       # same as Decimal('0.01')
>>> # Round to two places
>>> Decimal('3.214').quantize(TWOPLACES)
Decimal('3.21')
>>> # Validate that a number does not exceed two places
>>> Decimal('3.21').quantize(TWOPLACES, context=Context(traps=[Inexact]))
Decimal('3.21')
>>> Decimal('3.214').quantize(TWOPLACES, context=Context(traps=[Inexact]))
Traceback (most recent call last):
   ...
Inexact: None

The next question reads

Q. Once I have valid two place inputs, how do I maintain that invariant throughout an application?

If you need the answer to that (along with lots of other useful information), see the aforementioned section of the docs. Also, if you keep your Decimals with two digits of precision beyond the decimal point (meaning as much precision as is necessary to keep all digits to the left of the decimal point and two to the right of it and no more...), then converting them to strings with str will work fine:

str(Decimal('10'))
# -> '10'
str(Decimal('10.00'))
# -> '10.00'
str(Decimal('10.000'))
# -> '10.000'

这篇关于如何格式化小数以始终显示 2 个小数位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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