I have the below code for a calculation to be displayed in a label. The issue is with the WorkDurationHrsTextBoxMonthly.Text not outputting the value to conhrs. I think this would be due to the value of the textbox being a TimeSpan but I am not sure how
best to parse the value so I can display the result in a label.
WorkDurationHrsTextBoxMonthly.Text = 6:54
double conhrs, HrsPerMon;
double.TryParse(WorkDurationHrsTextBoxMonthly.Text, out conhrs); //Does not pass the value from WorkDurationHrsTextBoxMonthly.Text to conhrs
double.TryParse(WorkingHoursPerYearLabelMonthly.Text, out HrsPerMon);
double Perc = 100.00 * conhrs / HrsPerMon;
ConHrsUtilLabelMonthly.Text = Perc.ToString("c").Remove(0, 1); //Calculates the total and displays in label
Don't know anything about your application, but it looks like you are trying to calculate the percentage of time worked against the allocated hours for a particular month? If so, I'd be tempted to do the calculation in minutes:
var minsWorked = TimeSpan.Parse(WorkDurationHrsTextBoxMonthly.Text).TotalMinutes; var allowance = Double.Parse(WorkingHoursPerYearLabelMonthly.Text); // Assume total hours e.g. 160 rather than another timespan var percent = 100 * minsWorked / (allowance * 60);
Member
72 Points
251 Posts
Percentage Calculation
Oct 05, 2018 01:28 PM|jonnygareth30|LINK
Hi,
I have the below code for a calculation to be displayed in a label. The issue is with the WorkDurationHrsTextBoxMonthly.Text not outputting the value to conhrs. I think this would be due to the value of the textbox being a TimeSpan but I am not sure how best to parse the value so I can display the result in a label.
WorkDurationHrsTextBoxMonthly.Text = 6:54
double conhrs, HrsPerMon;
double.TryParse(WorkDurationHrsTextBoxMonthly.Text, out conhrs); //Does not pass the value from WorkDurationHrsTextBoxMonthly.Text to conhrs
double.TryParse(WorkingHoursPerYearLabelMonthly.Text, out HrsPerMon);
double Perc = 100.00 * conhrs / HrsPerMon;
ConHrsUtilLabelMonthly.Text = Perc.ToString("c").Remove(0, 1); //Calculates the total and displays in label
All-Star
190392 Points
27642 Posts
Moderator
Re: Percentage Calculation
Oct 05, 2018 01:59 PM|Mikesdotnetting|LINK
Don't know anything about your application, but it looks like you are trying to calculate the percentage of time worked against the allocated hours for a particular month? If so, I'd be tempted to do the calculation in minutes:
var minsWorked = TimeSpan.Parse(
WorkDurationHrsTextBoxMonthly.Text).TotalMinutes;
var allowance = Double.Parse(WorkingHoursPerYearLabelMonthly.Text); // Assume total hours e.g. 160 rather than another timespan
var percent = 100 * minsWorked / (allowance * 60);
Member
72 Points
251 Posts
Re: Percentage Calculation
Oct 08, 2018 11:50 AM|jonnygareth30|LINK
Thank you very much Mikesdotnett, worked perfectly!