Convert decimal (2.75) to time (2:45)

.net, c#

Solution

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

Problem

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

Original source

Related problems