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
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With