Dallin B - 25 days ago 6x

C# Question

I am new to C# and am sure my issue lies in the math, but I can't for the life of me figure it out.

What I need to do is take a time window on a 24hr clock ie: 1000-1100

convert it to seconds, which I can do.

(1000 / 100)*60 = 600

(1100 / 100)*60 = 660

Get 25% of the result(15), which I can also do. (660 / 4)

Add that 25% of additional seconds to the previous end time (1100)

The issue is that I am getting an output of 1100 instead of 1115

The Question is how do I make it output as 1115.

I don't know if I'm allowed to post my code here or not but its fairly brief. So if seeing it would help I can post it.

I do use constants to define hour and hundred so no magice numbers, these numbers are just illustrating the math.

`//define constants`

const int HUNDRED = 100;

const int HOUR = 60;

const int PERCENT = 4;

private void Form1_Load(object sender, EventArgs e)

{

}

private void button1_Click(object sender, EventArgs e)

{

// recieve start time input

string oldStart = oldStartTime.Text;

int oldTimeIn = int.Parse(oldStart);

// recieve end time input

string oldEnd = oldEndTime.Text;

int oldTimeEnd = int.Parse(oldEnd);

// convert start time to minutes

int hoursStart = (oldTimeIn / HUNDRED) * HOUR;

int minutesStart = oldTimeIn % HUNDRED;

int totalStart = hoursStart + minutesStart;

// convert end time to minutes

int hoursEnd = (oldTimeEnd / HUNDRED) * HOUR;

int minutesEnd = oldTimeEnd % HUNDRED;

int totalEnd = hoursEnd + minutesEnd;

// calulate 25% of travel time ( end - start )

int extraTime = totalEnd - totalStart;

int totalMinutes = (extraTime / PERCENT);

// convert back to hours

int newEndHours = (totalEnd + totalMinutes) / HOUR * HUNDRED;

// out put new end time

newEndTime.Text = $"{newEndHours:d4}";

Answer

```
int newEndHours = (totalEnd + totalMinutes) / HOUR * HUNDRED;
```

The operation is being done as integers, so the (660 + 15)/100 is coming out as 11. Remember that even if it was casted to doubles, it would result in 11.25, and not 11.15.

You will have to add in another part similar to when you deconstructed the time to handle the minutes part, so

```
int newEndHours = (totalEnd + totalMinutes) / HOUR * HUNDRED + (totalEnd + totalMinutes) % HOUR;
```

Source (Stackoverflow)

Comments