简体   繁体   English

在Python Decimal中使用Number Float格式

[英]Stuck with Number Float format in Python Decimal

I am working on a code and there I get data in the form of : 我正在编写代码,在那里我得到以下形式的数据:

k=[{'item_id': '43381702', 'length': {'_exp': -2, '_is_special': False, '_int': '0', '_sign': 0}, 'height': {'_exp': -2, '_is_special': False, '_int': '0', '_sign': 0}, 'width': {'_exp': -2, '_is_special': False, '_int': '0', '_sign': 0}, 'unitweight': {'_exp': -3, '_is_special': False, '_int': '3000', '_sign': 0}, 'quantity': 1}]

And It is expected that when I write 我写的时候有望

float(k[0]['unitweight'])

It should be converted into a float number . 它应该转换为浮点数。 But It is giving 但它正在给予

TypeError: float() argument must be a string or a number

as expected. 正如所料。 I am confused as the data is of the same form as is implemented inside python Decimal Module. 我很困惑,因为数据与python十进制模块中实现的格式相同。 Is there any way (one-liner) By which I can convert the data in float ? 有没有办法(单行)我可以通过它转换浮动数据?

This link has the same question probably . 这个链接可能有同样的问题。 Check out this also . 看看这个

Taking this: 考虑到这个:

d = {'_exp': -3, '_is_special': False, '_int': '3000', '_sign': 0}

for: 对于:

k[0]['unitweight']

You could do something like this: 你可以这样做:

import math  
math.copysign(int(d['_int']) ** d['_exp'], d['_sign'] * -1)

Result: 结果:

3.7037037037037036e-11

No idea what '_is_special' means. 不知道'_is_special'是什么意思。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM