How to convert python Decimal () types to INT and exponent

I would like to use the Decimal () data type in python and convert it to an integer and an exponent so that I can send this data to the / plc microcontroller with full precision control and decimal precision. https://docs.python.org/2/library/decimal.html

I have this to work, but these are hacks; Does anyone know a better way? If not for the path that I would take to write a lower function "as_int ()"?

Code example:

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 

For those who programmed the PLC, my alternative to this is to use int and declare a decimal point on both systems, or use a floating point (which only some PLCs support) and is lost. So you can understand why being able to do this would be great!

Thanks in advance!

+8
python decimal data-conversion microcontroller plc
source share
4 answers

You can do it:

[This is 3 times faster than other methods]

 d=Decimal('3.14159') list_d = str(d).split('.') # Converting the decimal to string and splitting it at the decimal point # If decimal point exists => Negative exponent # ie 3.14159 => "3", "14159" # exponent = -len("14159") = -5 # integer = int("3"+"14159") = 314159 if len(list_d) == 2: # Exponent is the negative of length of no of digits after decimal point exponent = -len(list_d[1]) integer = int(list_d[0] + list_d[1]) # If the decimal point does not exist => Positive / Zero exponent # 3400 # exponent = len("3400") - len("34") = 2 # integer = int("34") = 34 else: str_dec = list_d[0].rstrip('0') exponent = len(list_d[0]) - len(str_dec) integer = int(str_dec) print integer, exponent 

Performance testing

 def to_int_exp(decimal_instance): list_d = str(decimal_instance).split('.') if len(list_d) == 2: # Negative exponent exponent = -len(list_d[1]) integer = int(list_d[0] + list_d[1]) else: str_dec = list_d[0].rstrip('0') # Positive exponent exponent = len(list_d[0]) - len(str_dec) integer = int(str_dec) return integer, exponent def to_int_exp1(decimal_instance): t=decimal_instance.as_tuple() if t[0] == 0: sign=1 else: sign=-1 digits= t[1] exponent = t[2] integer = sign * int(''.join(map(str,digits))) return integer, exponent 
Calculation of time spent on 100,000 cycles for both methods:
 ttaken = time.time() for i in range(100000): d = Decimal(random.uniform(-3, +3)) to_int_exp(d) ttaken = time.time() - ttaken print ttaken 

Time spent on parsing method: 1.56606507301

 ttaken = time.time() for i in range(100000): d = Decimal(random.uniform(-3, +3)) to_int_exp1(d) ttaken = time.time() - ttaken print ttaken 

The time taken to convert to a tuple, then the extraction method: 4.67159295082

+3
source share
 from functools import reduce # Only in Python 3, omit this in Python 2.x from decimal import * d = Decimal('3.14159') t = d.as_tuple() theInteger = reduce(lambda rst, x: rst * 10 + x, t.digits) theExponent = t.exponent 
+2
source share

Get an exhibitor directly from a tuple like you:

 exponent = d.as_tuple()[2] 

Then multiply by the correct power of 10:

 i = int(d * Decimal('10')**-exponent) 

Putting it all together:

 from decimal import Decimal _ten = Decimal('10') def int_exponent(d): exponent = d.as_tuple()[2] int_part = int(d * (_ten ** -exponent)) return int_part, exponent 
+2
source share
 from decimal import * d=Decimal('3.14159') t=d.as_tuple() digits=t.digits theInteger=0 for x in range(len(digits)): theInteger=theInteger+digits[x]*10**(len(digits)-x) 
0
source share

All Articles