Click here to Skip to main content
16,020,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

Hey all

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  

    //TotalEmployeeTargetHours.Ticks = 4995000000<code>00


    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... 

 

What am I donig wrong?

 

Posted
Updated 27-Nov-09 4:13am
v2

1 solution

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.

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900