Click here to Skip to main content
15,887,398 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I am using FLEX version 3.0 I want to calculate the total working hours. I am giving you the details and what i have done in it.

var a:Number = 2.00;
var b:Number = 2.00;
var c:Number = 1.45;
var d:Number = 1.30;

var total:Number = a+b+c+d;
var totalmin:int = total;
var temp:Number = total - totalmin;
var tem2:Number = 0.60 - temp;
var workhour:Number = 0;

while(total >= 0)
{
   workhour += 1;
   total -= 1.00;
}

var tot:Number = workhour + tem2;

txtTotal.text = tot.toString();


I want result 7.15 not 6.75. currently i am getting 6.85... please Help to solve this issue.

thanks in advance.
Posted

Could it have anything to do with the fact that an hour contains 60 minutes, not 100? So 1 3/4 hours is not 1.45, but 1.75? and 1 1/2 hours is not 1.30 but 1.50?
 
Share this answer
 
Comments
Sagar Rawal 9-Dec-11 4:31am    
User can enter anything between 0 to 60. but that all in number that's why i am facing problem. he/she can enter 1.23 + 2.42 then total time is 4.05 not 3.65.....
OriginalGriff 9-Dec-11 4:40am    
I would convert the user input to mnutes, and do all work on that. Then convert back to hours and minutes at the end. What if you end up with four inputs of 1 hour, 45 minutes? The total is 7 hours, but your code only allows for one 60 minute overflow. Converting to minutes for everything solves this without complicating the code.
TorstenH. 9-Dec-11 4:39am    
you're right. Typical problem dealing with time.
I suggest to use Date objects, not Integer values. That solves the problem and gives a stable performance.
It's a bit more coding but worth it.
LanFanNinja 9-Dec-11 4:46am    
+5 I agree with this solution and the comments.
On this line
C#
var tem2:Number = 0.60 - temp;

you need to reverse the order of subtraction like so
C#
var tem2:Number = temp - 0.60;


this will give you a total of 7.15
 
Share this answer
 
Comments
Sagar Rawal 9-Dec-11 4:40am    
still not the right answer in following data
var a:Number = 2.33;
var b:Number = 2.00;
var c:Number = 1.45;
var d:Number = 1.45;
LanFanNinja 9-Dec-11 4:46am    
Sorry I was only going by the values posted in your question. I agree with the comments posted in solution 1 and I think you should rewrite this code using there suggestions.
I have worked a lot on this one and find one of the interesting questions..... and i have found the correct answer for that.. Keep posting that interesting questions.

here is the code.

var a:Number = 2.23;
var b:Number = 2.36;
var c:Number = 1.18;
var d:Number = 1.13;

var totalHours = 0.0;

var inta:int = a;
var numa:Number = a - inta;
var intb:int = b;
var numb:Number = b - intb;
var intc:int = c;
var numc:Number = c - intc;
var intd:int = d;
var numd:Number = d - intd;


var total:int = inta + intb + intc + intd;
var totalsec:Number = numa + numb + numc + numd;
var tempsec:int = totalsec * 100; 

var temp:int = tempsec/60;

total += temp;

var sec:Number = tempsec - (temp*60);

sec = sec/100;

totalHours = total + sec;

txtTotal.text = totalHours.toString();
 
Share this answer
 
Comments
Sagar Rawal 9-Dec-11 5:20am    
Thanks Manoj Savalia
LanFanNinja 9-Dec-11 5:34am    
+5

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