Saranya Rajendran
Saranya Rajendran

Reputation: 757

Converting Time to decimal

All i have to do that just find the overtime of employee. The attendance sheet comes in CSV file and I've already saved the data to a table. The data Field hours is 12:10:00.0000000, and The Working Hour per day is 11:00:00.0000000. I've calculated the difference between these two times using Timespan.

   DateTime date, hours, working_hours ;
   TimeSpan ot_hours = TimeSpan.Zero;
   TimeSpan tot_hours = TimeSpan.Zero;
   if (hours > working_hours)
    {
       ot_hours = (hours - working_hours);
     }

This code has set in a loop. after completing the loop I need to stote the total overtime into another variable. so wrote

 tot_hours += tot_hours + ot_hours;

And next I need to find the salary overtime amount for the employee. I've tried to convert this tot_hours(TimeSpan ) into Decimal. But it didn't work.

   tothrsvalue = Convert.ToDecimal(tot_hours);
   totalvalue = rate * tothrsvalue;

Anyone here.. Please have a look and help me.. Thanks in advance.

the total calculation part is given below:

         decimal basics = Convert.ToDecimal(dtamt.Rows[0]["Amount"].ToString());
                            decimal rate = 0;
                            rate = ((basics / 30) / 8);
                            txtrate.Text = rate.ToString("0.000");

                            tothrsvalue = Convert.ToDecimal(total);
                            totalvalue = rate * tothrsvalue;
                            txtamount.Text = totalvalue.ToString("0.000");
                            amt = Convert.ToDecimal(txtamount.Text);

Upvotes: 2

Views: 3656

Answers (3)

E. Spiroux
E. Spiroux

Reputation: 430

ot_hours is now a Timestamps difference = Seconds ! Divide them by 60 to make minutes

example : total = 2 minutes = 120 seconds etc.)

Upvotes: 0

Soner Gönül
Soner Gönül

Reputation: 98868

Parsing TimeSpan to decimal does not too much sense because time is not a numeric value at all.

But you can use it's TotalHours property which returns double.

var total = tot_hours.TotalHours;

Upvotes: 3

Dah Sra
Dah Sra

Reputation: 4445

Y need to to convert a TimeSpan to a decimal ? Its cannot be done becuase it isn't numeric.

You probably want tot_hours.TotalHours, which is a double that includes the fractional portion.

or tot_hours.TotalHours.ToString("#.00");

Upvotes: 1

Related Questions