Python pint我可以将默认类型设置为Decimal吗?

2024-09-29 17:14:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在项目中使用pint模块。我项目中的对象将数字数据处理为小数。当我将简单品脱单位设置为十进制时,它会起作用:

>>> import pint
>>> from decimal import Decimal as D
>>> ureg = pint.UnitRegistry()
>>> D(10) * ureg.kN
<Quantity(10, 'kilonewton')>

但是,如果我试着增加第二个单位,它就断了。在本例中,建筑千牛顿米:

^{pr2}$

我用的是:

>>> a = D(1) * ureg.kN
>>> b = D(1) * ureg.m
>>> unit_kNm = a * b
>>> D(10) * unit_kNm
<Quantity(10, 'kilonewton * meter')>

我明白为什么会这样。我正在寻找一个解决方案来建立品脱我想。在


Tags: 模块项目对象importunit单位数字quantity
2条回答

这是有效的:

>>> D(10) * (ureg.kN * ureg.m)
<Quantity(10, 'kilonewton * meter')>

还有这个:

^{pr2}$

类型转换为decimal

import decimal
D(10) * ureg.kN * decimal.Decimal(ureg.m)

相关问题 更多 >

    热门问题