First way:
long mySeconds = milliseconds/ 1000;
Second way:
double mySeconds = milliseconds * 1e-3d;
This calculation is finally used to determine index of an array, like this:
int index = (int) ((someDoubleSeconds + mySeconds)/ someDouble);
What difference would the two approaches make?
Also does the first approach rounds to the next second
or truncates like floor function
?
As others have pointed out, the first way truncates (rounds down, effectively, in this case) and the second way does not. So if
milliseconds
is 10999 then the first way gets you10
and the second gets you10.999
.What difference this makes in the third line? Well, consider if
someDoubleSeconds = 0
andsomeDouble = 10.5
. Then ifmilliseconds
is 10999, yourmySeconds
could be 10 or could be 10.999, and the result of the third line could be 0 or it could be 1. (since 10/10.5 is less than one, and would be truncated to 0 by the cast to int, and 10.999/10.5 is greater than one, and would be truncated to 1 by the cast to int)