NS 秒数 至 小时、分钟、秒
我在尝试做一些本应很容易的事情时遇到了困难。我的 NSNumber 值为 32025.89 秒。我需要用小时、分钟、秒来表示。有没有一种方法可以把它吐出来?我似乎找不到合适的格式化程序。
I am having a terrible time trying to do something that should be easy. I have a NSNumber value of 32025.89 seconds. I need to represent that in Hours, Minutes, Seconds. Is there a method that spits that out? I can't seem to find a proper formatter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果你不想把它分开,试试这个。对于您的目的来说,它可能足够接近。 注意:它不考虑亚秒精度。 (
setSecond
接受一个NSInteger
)。If you don't want to just divide it out, try this. It may be close enough for your purposes. Note: It doesn't account for sub-second precision. (
setSecond
takes anNSInteger
).您是否尝试过从中创建一个
NSDate
并使用NSDateFormatter
打印它?如果您需要小时、分钟和秒的单独值,请按照 nall 的建议使用
NSDateComponents
。如果
NSNumber
代表超过一天,上面的代码将不会打印正确的金额。可能是 1 天 1 小时,并且只会打印 01:00:00。在这种情况下,您应该手动计算小时、分钟和秒。Have you tried creating an
NSDate
from that and printing that using anNSDateFormatter
?If you need the individual values for hours, minutes, and seconds, use
NSDateComponents
as suggested by nall.The above won't print the correct amounts if the
NSNumber
represents more than one day. It could be 1 day and 1 hour, and it would only print 01:00:00. In such a case you should calculate hours, minutes and seconds manually.这是一种除了
math.h
中的modf()
之外不需要任何库的方法:Here's a way without any libraries except for
modf()
frommath.h
:NSNumber *valueForDisplay = [NSNumber numberWithDouble:
[self valueForDisplay:clockName]];
NSNumber *valueForDisplay = [NSNumber numberWithDouble:
[self valueForDisplay:clockName]];