C# 将十进制 (2.75) 转换为时间 (2:45)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15688802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert decimal (2.75) to time (2:45)
提问by jmasterx
If I have a double like 2.75, is there a way in .Net to format it as '2:45'
如果我有一个像 2.75 这样的双精度值,在 .Net 中有没有办法将它格式化为“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.
例如,如果是 2.75555555555,则应将其四舍五入到最接近的分钟。我不介意自己编码,但我想知道 .Net 是否可以。我检查了 ToString 但没有找到任何东西。
Thanks
谢谢
采纳答案by Sten Petrov
Use TimeSpan and its ToString formatter:
使用 TimeSpan 及其 ToString 格式化程序:
TimeSpan timespan = TimeSpan.FromHours(2.75);
string output = timespan.ToString("h\:mm");
For example
例如
TimeSpan.FromHours(2.75555).ToString("h\:mm")
outputs
产出
2:45
2:45