Friday, March 28, 2008

Convert Decimal to Hours & Minutes in C# .NET

Here is a simple code to convert a decimal value to its equivalent hour and minute value.


double
Value = 1.75;

double Hours = Math.Floor(Value);

double Minutes = (Value % 1 * 100);

Minutes = Math.Round(Minutes, 2);

if(Minutes >= 60)

{

Minutes = Math.Floor(Minutes/ 60) + (Minutes % 60 / 100);


Minutes = Math.Round(Minutes,2);

}

else

{

Minutes = Math.Round(Minutes / 100, 2);

}


string
HoursAndMinutes = Convert.ToString((Hours + Minutes));


12 comments:

joseph jelasker said...

Thanks a lot.

Anonymous said...

decimal tDec = Convert.ToDecimal(distance) / 25;
double Value = Convert.ToDouble(distance) / 25;
double Hours = Math.Floor(Value);
double Minutes = (Value % 1 * 100);
Minutes = Math.Round(Minutes, 2);
if (Minutes > Convert.ToDouble(0))
{
if (Minutes >= 60)
{
Hours = Hours + Math.Floor(Minutes / 60);
Minutes = Minutes % 60;
}
else
{
Minutes = Math.Round(60 / Minutes, 0);
}
}
else
{
Minutes = 0;
}
string HoursAndMinutes = Convert.ToString(Hours) + "h" + Convert.ToString(Minutes) + "m";
return HoursAndMinutes;

Thanks for the script, changed it a bit, this is for distances and speed... Also when the minutes are over sixty it adds the amount of hours on to hours...

Joe Hughes said...

Here is how I did it:

Decimal time = 1.75;
Decimal hours = Decimal.Round(time, 0);
Decimal minutes;

minutes = (hours == 0) ? time : (hours - time);
minutes *= Convert.ToDecimal(60);
minutes = Decimal.Round(minutes, 0);

Anonymous said...

This logic goes wrong when I enter -00.30 values

Anonymous said...

Thanks a lot
perfect solution

Anonymous said...

this is how i did it...

private TimeSpan DoubleToHours(double val)
{

double hours = Math.Floor(val);
double minutes = (val - hours) * 60.0;

int Hours = (int)Math.Floor(hours);
int Minutes = (int)Math.Floor(minutes);

TimeSpan nTime = new TimeSpan(Hours,Minutes,0);
return nTime;
}

Anonymous said...

Can someone please help:

I have two decimal text boxes on the ASP.Net page:

Balance: 200.00 (200 Hrs and 00 Minutes)
TextBox1 = 75.30 (75 Hrs and 30 Minutes)
TextBox2: (After entering the value in TextBox1; the function should calculate the difference between Balance - TextBox1 (based on 60 min per hour) = 114.30 (114 Hrs and 30 minutes)

Thanks in advance for your help

Varun Digumber said...

The best way in .Net would be as follows:
1. Convert your decimal to minutes or seconds
TimeSpan ts = TimeSpan.FromMinutes(value);
2. ts.Hours will give the hours, ts.Minutes in minutes.

Shimple!

Kr, Varun

Anonymous said...

Thank you very much......

Anonymous said...

but you can only convert values smaller than 24 using timespan.The first method is working very fine and thanks....

lee woo said...

We're still in the first minutes of the first day of the Internet revolution. See the link below for more info.


#minutes
www.ufgop.org

Silvia Jacinto said...

I really love your blog there's a lot to share. Keep it up.Visit my site too.

n8fan.net

www.n8fan.net