## 4 replies

Last post Apr 13, 2017 01:20 PM by hans_v

• jonnygareth3...

Member

46 Points

138 Posts

### Calculate hours from minutes (integer)

I have a label which displays the total of columns in a datagrid which is then used to display the total hours in a different label. The code below works but i would like to display this in an "hh:mm" format whereas the code i have rounds to the nearest hour (e.g. 75 minutes rounds to 1)

sum = 0;
for (int i = 0; i < CustomerReportDataGrid.Items.Count; i++)
{
sum += int.Parse(CustomerReportDataGrid.Items[i].Cells[9].Text);
}
TotalTravelStartMinutesLabel0.Text = sum.ToString();
h = sum / 60;
TravelWorkDurationHoursLabel.Text = h.ToString();

• paindaasp

Star

11739 Points

2987 Posts

### Re: Calculate hours from minutes (integer)

Try:

```int hrs = Convert.ToInt32(sum) / 60;
int mins = Convert.ToInt32(sum) % 60;
TravelWorkDurationHoursLabel.Text = hrs.ToString("D2") + ":" + mins.ToString("D2");```

• sakthivel053...

Member

250 Points

68 Posts

### Re: Calculate hours from minutes (integer)

Try this

```TotalTravelStartMinutesLabel0.Text = (int)(sum % 60);
TravelWorkDurationHoursLabel.Text = (int)(sum / 60);```

• jonnygareth3...

Member

46 Points

138 Posts

### Re: Calculate hours from minutes (integer)

Thanks for your help this has worked perfectly, I am also looking to do the same but calculating the total of a "money" value cost?

• hans_v

All-Star

25756 Points

7020 Posts

### Re: Calculate hours from minutes (integer)

```TravelWorkDurationHoursLabel.Text = TimeSpan.FromMinutes(sum).ToString("hh:mm");