相关疑难解决方法(0)

如何将pythons Decimal()类型转换为INT和exponent

我想在python中使用Decimal()数据类型并将其转换为整数和指数,以便我可以将数据发送到具有全精度和十进制控制的微控制器/ plc.https://docs.python.org/2/library/decimal.html

我有它工作,但它是hackish; 有谁知道更好的方法?如果不是我自己用什么路径写一个较低级别的"as_int()"函数?

示例代码:

from decimal import *
d=Decimal('3.14159')
t=d.as_tuple()
if t[0] == 0:
    sign=1
else:
    sign=-1

digits= t[1]
theExponent=t[2]
theInteger=sign * int(''.join(map(str,digits)))

theExponent
theInteger
Run Code Online (Sandbox Code Playgroud)

对于没有编程PLC的人,我的替代方法是使用int并在两个系统中声明小数点或使用浮点(只有一些PLC支持)并且是有损的.所以你可以看到为什么能够这样做会很棒!

提前致谢!

python microcontroller plc decimal data-conversion

10
推荐指数
2
解决办法
1795
查看次数

标签 统计

data-conversion ×1

decimal ×1

microcontroller ×1

plc ×1

python ×1