Hey all
I've got Two TimeSpan variables. HoursWorked and TargetHours.
HoursWorked
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);
float AbsentPercentage = 0;
if (TotalEmployeeTargetHours.Ticks != 0)
{
float ValA = (TotalEmployeeHoursWorked.Ticks / TotalEmployeeTargetHours.Ticks);
// TotalEmployeeHoursWorked.Ticks = 5073600000000
//TotalEmployeeTargetHours.Ticks = 4995000000<code>000
//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;
}
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...
ValA * 100;
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.
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)