开发者

Objective-C problems performing math on an integer

I have the following line of code which is running on a timer:

NSLog( @" seconds: %i, elapsedhours %f", [s开发者_高级运维elf 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


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..


An integer divided by an integer is an integer.

So 1/3600 is 0.

An integer divided by something else is something else

So 1/3600.0 is 0.00027777777

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜