tusharmakkar08
tusharmakkar08

Reputation: 696

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. 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 .

Upvotes: 0

Views: 277

Answers (1)

Mike Müller
Mike Müller

Reputation: 85462

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.

Upvotes: 2

Related Questions