准时双重格式化

Mar*_*ara 5 c# format double

如何设置TotalHours为双精度格式,或者需要做些什么才能得到txtBoxMonatstotal结果93.3。这是我的代码:

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

}
Run Code Online (Sandbox Code Playgroud)

当前结果如下:
  txtBoxMonatstotal

谢谢您的帮助

Mon*_*Zhu 1

您需要先对数字进行四舍五入ToString,然后再调用它

txtBoxMonatstotal.Text = System.Math.Round(workedPerMonth, 1).ToString();
Run Code Online (Sandbox Code Playgroud)

第二个参数Round决定

返回值中的小数位数。