Reputation: 4960
I would like to change 1762 milliseconds to seconds 1.762. I have tried the below method, it gives me seconds in single-digit when i need seconds as mentioned above (i.e "1.762").
seconds=(rcccc/1000)%60
sec = int(seconds)
Upvotes: 2
Views: 21452
Reputation: 2303
You just forgot to put .0
. The thing is when you divide that by 1000 it takes the floor function of the true value (1.762). If you want to get the true value you will have to represent 1000 as a decimal, for example 1000.0
. If you use that it will get the true value that you want (1.762).
Upvotes: 1
Reputation: 30813
This seems to be Python 2.x
, and this is caused by integer division. When bot the numerator and denominator are integer, the results will be integer:
seconds=(rcccc/1000)%60 #this is called integer division: int/int -> resulting in int
use dot (.
) in your number to avoid integer division, making it clear to the compiler that you do not want integer division, but floating point:
seconds=(rcccc/1000.0)%60
Alternatively, you could also cast one of the values to float
:
seconds=(rcccc/float(1000))%60
#or
seconds=(float(rcccc)/1000)%60
Upvotes: 1
Reputation: 76396
Out of the many ways to address this, one way would be to specify that one of the operands is a float.
>>> 1762 / 1000 # Both integers
1
>>> 1762 / 1000.0 # One float
1.762
Upvotes: 11