Ok so i need to take seconds and turn them into Hours, mins, and the leftover seconds. The problem is i dont know how to use the remainders(Ex: if i had 3.98 how would i use the .98) any help is appreciated.

3
Contributors
4
Replies
5
Views
13 Years
Discussion Span
Last Post by jerbo

Ok so i need to take seconds and turn them into Hours, mins, and the leftover seconds. The problem is i dont know how to use the remainders(Ex: if i had 3.98 how would i use the .98) any help is appreciated.

``````import cs1.Keyboard;

public class RT
{
public static void main(String[] args)
{
double si;

System.out.print("How many seconds?: ");

double hms = (si / 3600);

System.out.println(hms);

}
}
``````

i need to know how to use the remainder from hms

Edited by Nick Evan: Fixed formatting

I'm not too sure if this will work but, you can try some crazy casting.

double hms = (si / 3600);
int remainder = (int)hms;
double remainder2 = hms - (double)remainder;

Note: it's 4 a.m. so don't quote me on this. My linux machine with java on it is down so I can't even test it.

Use the modulas operator

remainingSeconds = (totalSeconds % 60);

This will give you seconds remaing.

;)

Even better:

int totalSec = 3645; // Sample number of seconds to change to HMS

int numHours = (int) (totalSec / 3600);

int numMin = (int) ((totalSec - (numHours * 3600)) / 60;

int numSec = (int) (totalSec % 60);

System.out.println(numHours + ":" + numMin + ":" + numSec);

numHours is dividing the total seconds by 3600 (number of seconds in an hour). Since we are doing an explicit cast to type Integer, the fraction part is truncated.

numMin takes the total seconds and subtracts (numHours * 3600) seconds to leave us just the remaining minutes. Again explicit casting to Integer to remove fractional part.

numSec does a modulus using 60 seconds per minute, and returns the remaining seconds. Again type casting to Integer.

:p

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.