views:

29

answers:

2

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

+4  A: 

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

Jack
Thanks guys that was it! I'm coming from the land of PHP where all this stuff is done magically. Difficult to get my head around these things.
Mark Steudel
A: 

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

Dave DeLong