本文介绍了如何将NSTimeInterval转换为int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将NSTimeInterval转换为整数值?

How do I convert NSTimeInterval into an Integer value?

我的TimeInterval保存值 83.01837 。我需要将其转换为 83 。我用谷歌搜索但找不到任何帮助。

My TimeInterval holds the value 83.01837. I need to convert it into 83. I have googled but couldn't find any help.

推荐答案

直接分配:

NSTimeInterval interval = 1002343.5432542;
NSInteger time = interval;
//time is now equal to 1002343

NSTimeInterval是一个双倍,所以如果你指定它直接到NSInteger(或int,如果你愿意)它会工作。这将把时间缩短到最接近的秒数。

NSTimeInterval is a double, so if you assign it directly to a NSInteger (or int, if you wish) it'll work. This will cut off the time to the nearest second.

如果你希望四舍五入到最接近的秒(而不是切断它),你可以在制作之前使用圆形作业:

If you wish to round to the nearest second (rather than have it cut off) you can use round before you make the assignment:

NSTimeInterval interval = 1002343.5432542;
NSInteger time = round(interval);
//time is now equal to 1002344

这篇关于如何将NSTimeInterval转换为int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:41