TimeSpan到整数 [英] TimeSpan to integer
本文介绍了TimeSpan到整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将TimeSpan
对象转换为等效的整数值?谢谢.
How can I convert a TimeSpan
object to an equivalent integer value? Thank you.
推荐答案
我知道这个问题早已被标记为答案了,但是我认为从另一个角度来看这可能很有趣. .
首先TimeSpan
在内部表示为long
(只需获取TimeSpan.Ticks
属性,鲍勃是您的叔叔),因此它已经是整数类型.
其次,您可以将其转换为十进制,仅用于编码练习:
I know this has halready had a response marked as the answer, but I was thinking it might be fun to look at it from another viewpoint...
FirstTimeSpan
is represented internally as along
(just get theTimeSpan.Ticks
property, and Bob''s your uncle), so it''s already an integer type.
Second, you could convert it to a decimal, just for the coding exercise:
public static class ExtensionMethods
{
//-------------------------------------------------------------
public static decimal ToDecimal(this TimeSpan span)
{
decimal spanSecs = (span.Hours * 3600) + (span.Minutes * 60) + span.Seconds;
decimal spanPart = spanSecs / 86400M;
decimal result = span.Days + spanPart;
return result;
}
//-------------------------------------------------------------
public static TimeSpan ToTimeSpan(this decimal value)
{
int days = Convert.ToInt32(Math.Ceiling(value));
value -= days;
int time = Convert.ToInt32(value * 86400M);
TimeSpan result = new TimeSpan(1, 0, 0, time, 0);
return result;
}
}
用法如下:
Usage would be thus:
TimeSpan span = new TimeSpan(0,12,0,0,0);
decimal spanAsDecimal = span.ToDecimal();
TimeSpan span2 = spanAsDecimal.ToTimeSpan();
有用?也许吧,也许不是.编码有趣吗?是的.
警告:这无需考虑毫秒,但添加起来很容易.
Useful? Maybe, maybe not. Fun to code? Yep.
Caveat: This doesn''t take milliseconds into account, but it would be easy to add.
我将不得不假设标签是错误的,并且您正在考虑.net System.TimeSpan结构.
如果是,则 System.TimeSpan.Ticks [
I''ll have to assume that the tag is wrong, and that you are thinking of the .net System.TimeSpan structure.
If you are, then System.TimeSpan.Ticks[^] is your answer.
int intValueLow32Bits = Convert.ToInt32( 0x00000000FFFFFFFF & ticksAsLong );
int intValueHigh32Bits = Convert.ToInt32( ticksAsLong >> 32 );
如果您正在考虑其他问题,我将需要更多信息...
问候
Espen Harlinn
If you are thinking about something else I''ll need more info ...
Regards
Espen Harlinn
这篇关于TimeSpan到整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文