Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you index a float value in Python?

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

Thanks in advance!

like image 256
Adam Spaniels Avatar asked Jul 03 '26 02:07

Adam Spaniels


1 Answers

In the comments to munkhd's answer you said:

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

like image 159
PM 2Ring Avatar answered Jul 04 '26 18:07

PM 2Ring