?
Solved

calc sum of hours problem

Posted on 2007-10-02
9
Medium Priority
?
394 Views
Last Modified: 2008-01-09
calc sum of hours
-------------------------
for exsmple i have 4 rows whit a time diff value :
7.3
7.3
7.3
7.3
its presnt 7 hours + 30 min
the resualt of the sum shold be : 30 hours
i am geting : 28.3
how can i fix that ?
thanks....

0
Comment
Question by:Tech_Men
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 11

Expert Comment

by:udhayakumard
ID: 19998771
Convert to datetime and then Add
0
 

Author Comment

by:Tech_Men
ID: 19998830
the code :
try
                  {
                        foreach(DataRow row in dsmas.Tables[0].Rows)
                        {
                              decimal num =Decimal.Parse(row["TotalHour"].ToString().IndexOf(".").ToString());
                              if(num == -1)
                              {
                                    resulte=new string[2];
                                    resulte[1]="0";
                                    tempBalance+=Convert.ToInt32(resulte[1]);

                              }
                              else
                              {
                                    resulte = row["TotalHour"].ToString().Split(tav);
                                    tempBalance+=Convert.ToInt32(resulte[1]);
                              }
                              resulte = row["TotalHour"].ToString().Split(tav);
                  
                              countHours+= Convert.ToInt32(resulte[0]);
                              
                              
                              if (tempBalance >= 60)
                              {
                                    countHours++;
                                    tempBalance -= 60;
                              }
                              else
                              {
                              
                                    tempBalance = Convert.ToInt32(resulte[1]);
                              }

                        }
                        string hoursresult = countHours.ToString() + "." + tempBalance.ToString();
                        editBox1.Text = hoursresult.ToString(); // ÜÔÑÙÐ èç Ðê ÔéâÕê
                  }
                  catch
                  {
                  }

where to convert ?
0
 
LVL 10

Expert Comment

by:dkloeck
ID: 19998847
that is because you are using floats  (hours do not have 100 minutes)
use:

public float Sum(float time1, float time2)
        {
            float temp = (float)((int)time1 + (int)time2);
            float time1min = time1 - ((int)time1);
            float time2min = time2 - ((int)time2);

            if (time1min + time2min >= 0.6)
                temp += 0.4f + time1min + time2min;
            else
                temp += time1min + time2min;
            return temp;
        }
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Tech_Men
ID: 19998898
sorry but i dont understand the func
can u gix my func that it work plesese ?
0
 
LVL 10

Expert Comment

by:dkloeck
ID: 19999107
if you use
public float Sum(float time1, float time2)
        {
            float temp = (float)((int)time1 + (int)time2);
            float time1min = time1 - ((int)time1);
            float time2min = time2 - ((int)time2);

            if (time1min + time2min >= 0.6)
                temp += 0.4f + time1min + time2min;
            else
                temp += time1min + time2min;
            return temp;
        }
to sum.. it will work
0
 

Author Comment

by:Tech_Men
ID: 19999160
foreach(DataRow row in dsmas.Tables[0].Rows)
                        {
                              decimal num =Decimal.Parse(row["TotalHour"].ToString().IndexOf(".").ToString());
                              if(num == -1)
                              {
                                    resulte=new string[2];
                                    resulte[1]="0";
                                    tempBalance+=Convert.ToInt32(resulte[1]);

                              }
                              else
                              {
                                    resulte = row["TotalHour"].ToString().Split(tav);
                                    tempBalance+=Convert.ToInt32(resulte[1]); // here i get the minutes only for the row from 7.30 i get 3
                              }
                              resulte = row["TotalHour"].ToString().Split(tav);
                  
                              countHours+= Convert.ToInt32(resulte[0]); //here i get the hour only 7 for the row
                              
                              
                              if (tempBalance >= 60)
                              {
                                    countHours++;
                                    tempBalance -= 60;
                              }
                              else
                              {
                              
                                    tempBalance = Convert.ToInt32(resulte[1]);
                              }

                        }
                        string hoursresult = countHours.ToString() + "." + tempBalance.ToString();
                        editBox1.Text = hoursresult.ToString();
0
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 2000 total points
ID: 19999195
System.Data.DataTable FTable =
                        new System.Data.DataTable();
                  FTable.Columns.Add("Hours");
                  for(int i=0;i<4;i++)
                        FTable.Rows.Add(new object [] {7.3});

//u can ignore till here it's just to populate data

                  double TotalHr = 0;
                  double TotalMin = 0;
                  foreach(System.Data.DataRow FRow in FTable.Rows)
                  {
                         AddTime(FRow["Hours"],ref TotalHr,ref TotalMin);
                  }
                  TotalHr += Math.Floor(Math.Round(TotalMin,0) /60);
                  TotalHr += ((Math.Round(TotalMin,0)%60)/100);


                  Console.WriteLine(TotalHr.ToString());



            static void AddTime(object Data,ref double TotalHr,ref double TotalMin)
            {
                  double Datadouble = Convert.ToDouble(Data);
                  double Hour = Math.Floor(Datadouble);
                  double Minute = (Datadouble - Hour)*100;
                  TotalHr += Hour;
                  TotalMin += Minute;
            }
0
 
LVL 4

Expert Comment

by:hclgroup
ID: 19999439
You could use 2 functions like

        static double ConvertHoursToDecimal(double h1)
        {
            return (int)h1 + ((h1 * 10 % 10) / 6);
        }
// 7.3 will return 7.5

        static double ConvertDecimalToHours(double h1)
        {
            return (int)h1 + ((h1 - (int)h1)*0.6);
        }

// 7.6 will return 7.36
0
 
LVL 4

Expert Comment

by:hclgroup
ID: 19999467
convert values first then use normal addition and then you can convert back to your format
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month15 days, 17 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question