Reputation: 59
I have a program that is a converter for times in minutes and seconds and returns a float value with a decimal, for example:
6.57312
I would like to extract the .57312
part in order to convert it to seconds.
How can I get python to take only the value after the decimal point and put it into a variable that I can then use for the conversion?
Upvotes: 4
Views: 2365
Reputation: 168866
math.modf
does that. It also has the advantage that you get the whole part in the same operation.
import math
f,i = math.modf(6.57312)
# f == .57312, i==6.0
Example program:
import math
def dec_to_ms(value):
frac,whole = math.modf(value)
return "%d:%02d"%(whole, frac*60)
print dec_to_ms(6.57312)
Upvotes: 7