Tech_Men
asked on
calc sum of hours problem
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....
-------------------------
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....
Convert to datetime and then Add
ASKER
the code :
try
{
foreach(DataRow row in dsmas.Tables[0].Rows)
{
decimal num =Decimal.Parse(row["TotalH our"].ToSt ring().Ind exOf("."). ToString() );
if(num == -1)
{
resulte=new string[2];
resulte[1]="0";
tempBalance+=Convert.ToInt 32(resulte [1]);
}
else
{
resulte = row["TotalHour"].ToString( ).Split(ta v);
tempBalance+=Convert.ToInt 32(resulte [1]);
}
resulte = row["TotalHour"].ToString( ).Split(ta v);
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 ?
try
{
foreach(DataRow row in dsmas.Tables[0].Rows)
{
decimal num =Decimal.Parse(row["TotalH
if(num == -1)
{
resulte=new string[2];
resulte[1]="0";
tempBalance+=Convert.ToInt
}
else
{
resulte = row["TotalHour"].ToString(
tempBalance+=Convert.ToInt
}
resulte = row["TotalHour"].ToString(
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 ?
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;
}
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;
}
ASKER
sorry but i dont understand the func
can u gix my func that it work plesese ?
can u gix my func that it work plesese ?
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
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
ASKER
foreach(DataRow row in dsmas.Tables[0].Rows)
{
decimal num =Decimal.Parse(row["TotalH our"].ToSt ring().Ind exOf("."). ToString() );
if(num == -1)
{
resulte=new string[2];
resulte[1]="0";
tempBalance+=Convert.ToInt 32(resulte [1]);
}
else
{
resulte = row["TotalHour"].ToString( ).Split(ta v);
tempBalance+=Convert.ToInt 32(resulte [1]); // here i get the minutes only for the row from 7.30 i get 3
}
resulte = row["TotalHour"].ToString( ).Split(ta v);
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();
{
decimal num =Decimal.Parse(row["TotalH
if(num == -1)
{
resulte=new string[2];
resulte[1]="0";
tempBalance+=Convert.ToInt
}
else
{
resulte = row["TotalHour"].ToString(
tempBalance+=Convert.ToInt
}
resulte = row["TotalHour"].ToString(
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();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You could use 2 functions like
static double ConvertHoursToDecimal(doub le h1)
{
return (int)h1 + ((h1 * 10 % 10) / 6);
}
// 7.3 will return 7.5
static double ConvertDecimalToHours(doub le h1)
{
return (int)h1 + ((h1 - (int)h1)*0.6);
}
// 7.6 will return 7.36
static double ConvertHoursToDecimal(doub
{
return (int)h1 + ((h1 * 10 % 10) / 6);
}
// 7.3 will return 7.5
static double ConvertDecimalToHours(doub
{
return (int)h1 + ((h1 - (int)h1)*0.6);
}
// 7.6 will return 7.36
convert values first then use normal addition and then you can convert back to your format