【问题标题】:Converting Time to decimal将时间转换为十进制
【发布时间】:2016-05-09 17:06:22
【问题描述】:

我所要做的就是找到员工的加班时间。考勤表采用 CSV 文件格式,我已经将数据保存到表格中。数据 Field hours 为 12:10:00.0000000,The Working Hour per day 为 11:00:00.0000000。我已经使用时间跨度计算了这两次之间的差异。

   DateTime date, hours, working_hours ;
   TimeSpan ot_hours = TimeSpan.Zero;
   TimeSpan tot_hours = TimeSpan.Zero;
   if (hours > working_hours)
    {
       ot_hours = (hours - working_hours);
     }

此代码已设置为循环。完成循环后,我需要将总加班时间记入另一个变量。这么写

 tot_hours += tot_hours + ot_hours;

接下来我需要找到员工的加班工资金额。 我试图将此 tot_hours(TimeSpan ) 转换为十进制。但它没有用。

   tothrsvalue = Convert.ToDecimal(tot_hours);
   totalvalue = rate * tothrsvalue;

这里的任何人..请看看并帮助我..提前谢谢。

总计算部分如下:

         decimal basics = Convert.ToDecimal(dtamt.Rows[0]["Amount"].ToString());
                            decimal rate = 0;
                            rate = ((basics / 30) / 8);
                            txtrate.Text = rate.ToString("0.000");

                            tothrsvalue = Convert.ToDecimal(total);
                            totalvalue = rate * tothrsvalue;
                            txtamount.Text = totalvalue.ToString("0.000");
                            amt = Convert.ToDecimal(txtamount.Text);

【问题讨论】:

  • 我预计tot_hours += tot_hours + ot_hours; 是错误的,因为这基本上意味着加倍总数,然后加上加班。如果总数为0,那么您不会注意到这是一个问题,但在其他情况下会导致不正确的结果。你应该只使用tot_hours += ot_hours;tot_hours = tot_hours + ot_hours;
  • 是的.. 谢谢@musefan .. 那是我的错误。我已经更正了

标签: c# asp.net datetime decimal type-conversion


【解决方案1】:

TimeSpan 解析为decimal 没有太大意义,因为时间根本不是数值

但是你可以使用它的TotalHours property,它返回double

var total = tot_hours.TotalHours;

【讨论】:

  • 我也试过了.. total = tot_hours.TotalHours;
  • 好的。其他变量的类型为十进制。
【解决方案2】:

Y 需要将 TimeSpan 转换为小数吗?它不能完成,因为它不是数字。

您可能需要tot_hours.TotalHours,它是包含小数部分的双精度数。

tot_hours.TotalHours.ToString("#.00");

【讨论】:

    【解决方案3】:

    ot_hours 现在是 时间戳差异 = 秒!将它们除以 60 得到分钟

    示例:总计 = 2 分钟 = 120 秒等)

    【讨论】:

      猜你喜欢
      • 2015-03-12
      • 2017-04-18
      • 1970-01-01
      • 1970-01-01
      • 2021-07-22
      • 2011-07-09
      • 2014-03-13
      • 2018-01-24
      • 1970-01-01
      相关资源
      最近更新 更多