double Value = 1.75;

double Minutes = (Value % 1 * 100);

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:

Thanks a lot.

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...

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);

This logic goes wrong when I enter -00.30 values

Thanks a lot

perfect solution

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;

}

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

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

Thank you very much......

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

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

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

n8fan.net

www.n8fan.net

Post a Comment