Converting Raw Seconds to 00:00:00 format

Discussion in 'iOS Programming' started by hellrider, Jul 9, 2009.

  1. hellrider macrumors regular

    Jun 7, 2008
    I have an NSTimer that spits out a float value every 1/16th of a second. (Every 1/16th of a second, 1/16 of a second is taken off the float value).

    The application functions as a countdown timer, so I want the raw fractional seconds (i.e. 9.9375) to be converted into (hours:minutes:seconds:000).

    This is somewhat hard to explain, and I apologize, but right now, I have an NSString that looks like 9.xxxx; my goal is for it to look like 00:00:9:xx:xx.

    If this is possible, I would appreciate some guidance.

  2. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    The usual way to do this is by a combination of truncation, subtraction, and multiplication.

    9.9375 truncated gives 9
    9.9375 - 9 gives 0.9375
    0.9375 times 100 gives 93.75
    93.75 truncated gives 93
    and so on.

    Put together your string with these intermediate results.

    You use a similar process to get hours:minutes:seconds. Divide by secondsPerHour to get hours. Etc.

    BTW, timers aren't guaranteed to fire exactly on the times that they've been told to fire. It's considered better to subtract the current time from the start time to get the elapsed time rather than simply increment/decrement the time.

Share This Page