Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double-formatting on time

Tags:

c#

format

double

How can I set TotalHours to be in double format, or what I need to do to get the result in txtBoxMonatstotal as a result of 93.3. This is my code:

private void calendar1_MonthChanged(object sender, EventArgs e)
{
    DateTime start = new DateTime(calendar1.CurrentDate.Year, calendar1.CurrentDate.Month, 1);
    DateTime stop = new DateTime(calendar1.CurrentDate.Year, calendar1.CurrentDate.Month, 1).AddMonths(1).AddDays(-1);
    int numberOfWorkDays = GetNumberOfWorkingDays(start, stop);

    double shouldWorkPerMonth = tag_durschnit * numberOfWorkDays;
    double workedPerMonth = workingHours.Where(x => x.Key.Date.Year == start.Year && x.Key.Month == start.Month).Sum(x => x.Value.TotalHours);
    double saldo = workedPerMonth - shouldWorkPerMonth;

    txtBoxMonatstotal.Text = workedPerMonth.ToString();
    txtBoxSollzeit.Text = shouldWorkPerMonth.ToString();
    txtBoxSaldo.Text = saldo.ToString();

}

The current result looks like this:
  txtBoxMonatstotal

Thank you for your help

like image 570
Mara Avatar asked Nov 08 '22 11:11

Mara


1 Answers

You need to round the number first and then call ToString on it

txtBoxMonatstotal.Text = System.Math.Round(workedPerMonth, 1).ToString();

the second parameter in Round determines

the number of fractional digits in the return value.

like image 61
Mong Zhu Avatar answered Nov 15 '22 07:11

Mong Zhu