Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert decimal (2.75) to time (2:45) [duplicate]

Tags:

If I have a double like 2.75, is there a way in .Net to format it as '2:45'

If it is for example, 2.75555555555, it should round it to the nearest minute. I would not mind coding this myself, but I am wondering if .Net can. I checked ToString but did not find anything.

Thanks

like image 822
jmasterx Avatar asked Mar 28 '13 17:03

jmasterx


People also ask

What is the fraction for 2.75 decimal?

So as a simplified mixed number, this becomes 2 and 3/4. And after you do a lot of practice here, and you just see a lot of numbers like this, it will be almost second nature for you to say, oh, 2.75 is the same thing as 2 and 75/100, is the same thing as 2 and 3/4.

How do you convert decimal hours to hours?

If you have time value in decimals and you want to convert it into hours, all you have to do is multiply it by 24. Note that 24 here represents 24 hours, the number of hours in a day.


1 Answers

Use TimeSpan and its ToString formatter:

TimeSpan timespan = TimeSpan.FromHours(2.75); string output = timespan.ToString("h\\:mm"); 

For example

TimeSpan.FromHours(2.75555).ToString("h\\:mm") 

outputs

2:45

like image 56
Sten Petrov Avatar answered Sep 22 '22 03:09

Sten Petrov