0

So now I have a variable which is x = 1001.0010101

From this x, I wanna separate into two parts:

x = 1001.0010101
val_int = int(x)                         #get val_int   = 1001
val_fract = {0:.5f}".format(a - val_int) #get val_fract = 0.00101

Is it possible to use for loop to iterate the val_fract to be like: (ignore the int part and decimal point)

0 
0 
1 
0 
1

I have tried so many times and I couldn't get it done and the system told me

Traceback (most recent call last):
  File "python", line 46, in <module>
TypeError: 'float' object is not iterable

Thanks for your help, much appreciated.

  • 1
    You might want to convert the variable into a string object and then use regular expressions to get what you want. – baduker Mar 05 '18 at 10:11
  • I just tried using a split method to separate two parts. However, the decimal part has not been fully split, what I meant is the second part would only take 10101 and **00**(these two zeros)10101 are missing. – Yashida Kim Lee Roger Mar 05 '18 at 10:16
  • What is your code for the split appraoch, can you append it to your question? – FlyingTeller Mar 05 '18 at 10:17
  • 1
    Can you explain more what this is for? It's an odd thing to be doing, and I suspect there's a better way to solve whatever the underlying problem is. Note that the value `1001.0010101` *can't be represented* in a Python `float`. The best you can get is `1001.001010100000030433875508606433868408203125` instead. Do you want to see all those digits in the fractional part? If not, what's your criterion for truncating them? – Mark Dickinson Mar 05 '18 at 11:39
  • Actually I'm doing a binary converter, so that's why the digits are in 1 and 0, so I don't need any other numbers, so I could just limit the length of the decimal part. – Yashida Kim Lee Roger Mar 05 '18 at 11:41
  • 1
    Then you definitely shouldn't be storing `1001.0010101` in a Python `float`. You should be representing it as a string, not a number. – Mark Dickinson Mar 05 '18 at 11:41

3 Answers3

1

You can use math module in python to separate decimal and integer part

import math 
x = 1001.0010101
math.modf(x)
#output:(0.0010101000000304339, 1001.0)

Iterate as you want

Have doubt about extra numbers in end of decimal read docs

Shubhitgarg
  • 588
  • 6
  • 20
1

I don't know, why you suggest in your comment that leading zeros are missing:

x = 1001.0010101
#separate fractional from integer part
frac = str(x).split(".")

for digit in frac[1]:
    print(digit)

Alternatively, you can transform both parts into lists of integers:

#integer digits
x_int = list(map(int, frac[0]))
#fractional digits
x_frac = list(map(int, frac[1]))
Mr. T
  • 11,960
  • 10
  • 32
  • 54
  • Thanks man! Works fine! Because I was just like converting the decimal part using `num = 123.456 split_num = str(num).split('.') int_part = int(split_num[0]) decimal_part = int(split_num[1])` Please refer to https://stackoverflow.com/questions/6681743/splitting-a-number-into-the-integer-and-decimal-parts-in-python It just has its disadvantages using this method. – Yashida Kim Lee Roger Mar 05 '18 at 10:23
1
x = 1001.0010101
x = "{0:.5f}".format(x)
for i in str(x).split(".")[1]:
    print(i)

Output:

0
0
1
0
1
Rakesh
  • 81,458
  • 17
  • 76
  • 113