How do you index a float value in Python?

How do you index a float value in Python?

Contents

Problem Description:

Just looking for an answer to a question no amount of googling appears to resolve.

if..

``````a = 1.23
``````

I would like to be able to take the 1 and multiply this number yet keep the .23

How is this possible??

Solution – 1

Is `int()` what you’re looking for:

``````In [39]: a = 1.23

In [40]: int(a)
Out[40]: 1

In [41]: a
Out[41]: 1.23
``````

Solution – 2

You can’t index a float as such. It’s not a collection. For more info on how floating point numbers work, read http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

The math library gives you a way to get a tuple with the two parts of the number though

``````>>> import math
>>> a = 1.23
>>> math.modf(a)
(0.22999999999999998, 1.0)
``````

This is not exacct because of the way floating point numbers are represented, but the value is on the order of 1e-17.

Of course, this is always possible too…

``````>>> map(int, str(a).split('.'))
[1, 23]
``````

Solution – 3

I have want to be able to input a value as hours then convert them to minutes.
So if it was 1.20 I would multiply the 1 by 60 then add 20.
Im sure there must be an easier method 🙂

Thus your program will receive 1.20 as a string. So you can use string methods on it, eg

``````>>> dotted_time = '1.20'
>>> h, m = [int(s) for s in dotted_time.split('.')]
>>> print h, m
1 20
>>> minutes = 60*h + m
>>> hours = minutes / 60.0
>>> print minutes, hours
80 1.33333333333
``````

Alternatively, you can do `colon_time = dotted_time.replace('.', ':')`; `colon_time` is in a format that the standard time functions can understand and manipulate for you. This is probably the more sensible way to proceed, and it will easily cope if you want to process times with seconds like ‘1.20.30’ which `.replace('.', ':')` will convert to ‘1:20:30’.

Solution – 4

Besides PM 2Ring’s answer seems to solve [1] your actual problem, you may “index floats”, of course after converting it to strings, but be aware of the limited accuracy. So use the built-in `round` function to define the accuracy required by your solution:

``````s = str(round(a, 2)) # round a to two digits
``````

Now, you may apply all the string functions you want to it. After that you probably need to convert your results back to `float` or `int`

[1] Others already suggested this: when dealing with common models like time in a high-level language like python, expect to be supported by reliable solutions that are already there.

modf() and round()

Use `modf()` to get the whole number and fractional part. `modf()` will return a tuple of (fractional, whole). Use `round()` to account for how precise you want the answer.

``````>>> import math
>>> a = 1.23
>>> math.modf(a) (0.22999999999999998, 1.0)
>>> math.modf(a)[0] #the zeroth location of the tuple is the fractional part
0.22999999999999998 # some precision lost, use round() to fix
>>> round(math.modf(a)[0], 2)
0.23 # This is an exact match the source fractional part
``````

Further, if you want to index each specific portion of the float you can use this:

``````def get_pos_float(num, unit, precision=3):
if unit >= 10:
num = abs(round(math.modf(num)[0], 3)) # Get just the fractional part
num *= 10 # Move the decimal point one place to the right
return get_pos_float(num, unit/10)
retVal = int(math.modf(num)[1]) #Return the whole number part
return retVal
``````

It is used as follows:

``````>>> get_pos_float(1.23, 10)
2
>>> get_pos_float(1.23, 100)
3
``````
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.