本文介绍了计算缺席时间问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个TimeSpan变量. HoursWorked TargetHours.

I''ve got Two TimeSpan variables. HoursWorked and TargetHours.

现在,我需要计算缺席时间的百分比.所以这是代码:

Now what I need is to calculate the percentage of absent time. So here''s the code:

 

float AbsentPercentage = 0;

if (TotalEmployeeTargetHours.Ticks != 0)
{

   float ValA = (TotalEmployeeHoursWorked.Ticks / TotalEmployeeTargetHours.Ticks);

      // TotalEmployeeHoursWorked.Ticks  = 5073600000000  

      // TotalEmployeeHoursWorked.Ticks  = 5073600000000  

     //TotalEmployeeTargetHours.Ticks = 4995000000 <code>00

    //TotalEmployeeTargetHours.Ticks = 4995000000<code>00


    float ValB = ValA * 100;
    float ValC = 100 - ValB;


    AbsentPercentage = ValC;
}   


    float ValB = ValA * 100;
    float ValC = 100 - ValB;


    AbsentPercentage = ValC;
}   

应该可以,对吧? ValA总是被分配0或1.所以我的缺席百分比始终是100%或0%

That should work, right? ValA ALWAYS gets assigned either 0 or 1. So my absent percentage is always 100% or 0%

 

我先尝试使用小数然后尝试了浮点数,我认为可能会在后台发生某种类型的转换... 

I tried using decimal first and then tried floats, i thought there might be some kind of cast happening in the background... 

 

 

推荐答案

 

刻度不是浮点数.您需要将TotalEmployeeTargetHours.Ticks强制转换为浮点数,以获取浮点结果.否则,它会向上或向下取整,然后将THEN分配给浮点值.

Ticks is not a float.  You need to cast TotalEmployeeTargetHours.Ticks to float, to get a floating point result.  Otherwise it gets rounded up or down, and THEN gets assigned to the floating point value.


这篇关于计算缺席时间问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 08:01