locked
Percentage Calculation RRS feed

  • Question

  • User-1901014284 posted

    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

    Friday, October 5, 2018 1:28 PM

Answers

  • User-821857111 posted

    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);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 5, 2018 1:59 PM

All replies

  • User-821857111 posted

    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);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 5, 2018 1:59 PM
  • User-1901014284 posted

    Thank you very much Mikesdotnett, worked perfectly!

    Monday, October 8, 2018 11:50 AM