对整数执行数学运算的 Objective-C 问题
我有以下在计时器上运行的代码行:
NSLog( @" seconds: %i, elapsedhours %f", [self elapsedSeconds], [self elapsedSeconds] / 3600);
它打印出:
seconds: 0, elapsedhours 0.000000
seconds: 1, elapsedhours 0.000000
seconds: 2, elapsedhours 0.000000
seconds: 3, elapsedhours 0.000000
我想知道为什么经过的时间不更新?
elapsedSeconds 方法:
- (NSUInteger)elapsedSeconds;
{
NSUInteger seconds = 0;
if( ![self endDate] ) {
seconds = [[NSDate date] timeIntervalSinceDate: [self startDate]];
NSLog( @" startdate = %@ no end date %i", startDate, seconds );
}
else {
seconds = [[self endDate] timeIntervalSinceDate: [self startDate]];
}
return seconds;
}
你们还需要看什么吗?
TIA
I have the following line of code which is running on a timer:
NSLog( @" seconds: %i, elapsedhours %f", [self elapsedSeconds], [self elapsedSeconds] / 3600);
It prints out:
seconds: 0, elapsedhours 0.000000
seconds: 1, elapsedhours 0.000000
seconds: 2, elapsedhours 0.000000
seconds: 3, elapsedhours 0.000000
I'm wondering why elapsed hours doesn't update?
Method for elapsedSeconds:
- (NSUInteger)elapsedSeconds;
{
NSUInteger seconds = 0;
if( ![self endDate] ) {
seconds = [[NSDate date] timeIntervalSinceDate: [self startDate]];
NSLog( @" startdate = %@ no end date %i", startDate, seconds );
}
else {
seconds = [[self endDate] timeIntervalSinceDate: [self startDate]];
}
return seconds;
}
Anything else you guys/gals need to see?
TIA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为您使用的是 整数除法,它不会计算小数部分,尽管您随后将其打印为漂浮。
如果您想查看部分结果,您应该将其除以
3600.0
。Because you are using integer division, which doesn't calculate decimal parts although you then print it as a float.
You should divide it by
3600.0
if you want to see partial results..一个整数除以一个整数就是一个整数。
所以
1/3600
是0
。整数除以其他值是其他值,
因此
1/3600.0
是0.00027777777
An integer divided by an integer is an integer.
So
1/3600
is0
.An integer divided by something else is something else
So
1/3600.0
is0.00027777777