转换一个浮动到一个时间跨度 [英] Converting a float into a timespan

查看:133
本文介绍了转换一个浮动到一个时间跨度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有采取浮动值,表示分数小时,并将其转换成时间跨度对象的简单方式反之亦然?例如:

Is there a simple way to take a float value representing fractional hours and convert them into a TimeSpan object and visa versa? For example:

float oneAndAHalfHours = 1.5f;
float twoHours = 2f;
float threeHoursAndFifteenMinutes = 3.25f;
float oneDayAndTwoHoursAndFortyFiveMinutes = 26.75f;

TimeSpan myTimeSpan = ConvertToTimeSpan(oneAndAHalfHours); // Should return a TimeSpan Object



任何想法?

Any ideas?

推荐答案

您想 FromHours 方法。

这需要一个双(而不是浮动),并返回一个时间跨度

This takes a double (rather than a float) and returns a TimeSpan:

double hours = 1.5;
TimeSpan interval = TimeSpan.FromHours(hours);

要从得到总小时时间跨度使用`TotalHours'属性:

To get the total hours from a TimeSpan use the `TotalHours' property:

TimeSpan interval = new TimeSpan(1, 15, 42, 45, 750);
double hours = interval.TotalHours;

这篇关于转换一个浮动到一个时间跨度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆