I think you should keep nanoseconds as long and not convert it into float seconds.
then you'll have code like this: timeElapsed is defined as long:
long timeElapsed = 0;
End of your loop will look like this:
timeBefore = System.nanoTime();
loopCall(timeElapsed);
timeElapsed =(System.nanoTime()-timeBefore);
while(timeElapsed < 5000000){
timeElapsed = (System.nanoTime()-timeBefore);
}
I hope that's what you're looking for.
Also I'd recommend to do waiting with Thread.sleep(long, int); You'll lose some precision (it sleeps for milliseconds) but will save some CPU time
/*while(timeElapsed < 5000000){
timeElapsed = (System.nanoTime()-timeBefore);
}*/
long leftToSleep = 5000000 - timeElapsed;
if(leftToSleep > 0) {
//dont forget to surround it with try catch
Thread.sleep(leftToSleep / 1000000, (int) leftToSleep % 1000000);
}