0

I am trying to show the percentage of day passed using a fixed time. However, when I divide the time passed already by the total amount of time (in seconds) of a day, I get 0.0. I put the current values into the console. Any help is appreciated.

1 Answers1

1

You are performing integer division, and then casting it to a double. You should be doing:

int numOfSecondsSinceMidnight = 61960;
int totalDay = 86400;
double percentDayPassed = 0;
percentDayPassed = (((double)numOfSecondsSinceMidnight / totalDay)*100);
System.out.println(percentDayPassed);

Or better yet, changing numOfSecondsSinceMidnight and totalDay to doubles:

double numOfSecondsSinceMidnight = 61960;
double totalDay = 86400;
double percentDayPassed = 0;
percentDayPassed = ((numOfSecondsSinceMidnight / totalDay)*100);
System.out.println(percentDayPassed);

Both of which print:

71.71296296296296
Spectric
  • 5,761
  • 2
  • 6
  • 27