How to Convert pythons Decimal() type into an INT and exponent -


I would like to use the decimal () data type in the python and I would like to convert it to an integer and exponent so that I send it Can a microcontroller / plc data with full precision and decimal control?

I have got to work, but this is a hack; Anyone know a better way? If not, what path would I take to write the less written "as_int ()" function? Example code:

  From decimal import * d = decimal ('3.14159') t = ds_tplay () if t [0] == 0: mark = 1 Other: Icon = -1 Points = T [1] Express = T [2] The Engineer = Sign * Int (''. Map (str, digits)) TheExponent theInteger   

For those that have programmed PLCs havent, to do this, use their option in the integer and to declare a decimal point in both systems or use the floating point (which only support some PLC) And lossy. So you can see why it would be awesome to do this!

Thank you in advance!

You can:

[This is 3 times faster than other ways S]

  d = decimal ('3.14159') list_d = str (d)) .split ('.') # Splitting the decimal to string at decimal point # if the decimal point exists Is & gt; Negative exponent # i. 3.14159 = & gt; "3", "14,159" #Applifier = -len ("14,159") = -5 # integer = integer ("3" + "14,159") = 314,159 if lane (list_d) == 2: # is negative of the exponent decimal After the digit exponent = -lan (list_d [1]) integer = int (list_d [0] + list_d [1]), the length of the number of digits is # if the decimal point is not present then => Positive / zero exponent # 3400 #Appliant = Lane ("3400") - Lane ("34") = 2 # integer = integer ("34") = 34 left: str_dec = list_d [0] .rstrip ('0') Exponent = lane (list_d [0]) - lane (str_dec) integer = integer (str_dec) print integer, exponent   

performance test
  def to_int_exp ( Decimal_instance): list_d = str (decimal_instance) .split (), then lane (list_d) == 2 '.': # Negative expression exponent = -len (list_d [1]) integer = integer (list_d [0] + List_d [1]) and: str_dec = list_d [0] .rstrip ('0') # positive copy Adak exponent = lane (list_d [0]) - lane (str_dec) integer = integer (str_dec) return integer, exponent def to_int_exp1 (decimal_instance): T = decimal_instance.as_tuple () if t [0] == 0: mark = 1 Other: mark = -1 points = t [1] exponent = t [2] integer = mark * int (''. Calculation of time taken for 100,000 loops for both methods: 
  ttaken = time For the (100000) range in the .time () range: integer, return   return integer, exponent d = decimal (random.uniform (-3, +3)) to_int_exp (d) ttaken = time .tai () - ttaken print ttaken   

Time taken for string parsing method: 1.56606507301

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

Time taken for conversion Tupl then remove method: 4.67159295082

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -