1

I'm currently trying to convert xml values that represents two times.

example:

<from>960</from>
<to>975</to>

i store these 2 values into two doubles:

double From = from;
double To = to;

Now, if i do

From=From/60;
To=To/60;

and

NSString *fromString = [NSString stringWithFormat:@"%f",From];
NSString *toString = [NSString stringWithFormat:@"%f",To];

i thereafter smash these together to one string separated by " - " and NSLog the new string:

NSLog(@"New String: %@",FromToString);

The output: 16:00-16:25

Now, what i'm trying to accomplish here is for the FromToString to get the value 16:00 - 16:15

So, the question is how do i recalculate this to represent hours in 60minutes instead of percent of an hour? O_o

Jens Bergvall
  • 1,617
  • 2
  • 24
  • 54

2 Answers2

1

So all you want is to have a double (eg. 9.5) converted to a string (in this case: 09:30), right?

You can do it like that:

// get hours
NSInteger hours = (int)floor(number);

// get minutes
NSInteger minutes = (int)floor((number-hours)*60);

// construct string
NSString* timeString = [NSString stringWithFormat: @"%02d:%02d", hours, minutes];

cheers (not tested though)

calimarkus
  • 9,955
  • 2
  • 28
  • 48
1

you will need two variables, something like

fromHours = floor(from/60);
fromMinues = from % 60;
NSString fromTime = [NSString stringWithFormat:"%02f:%02f", 
                                               fromHours, fromMinues);

and the same for the other variable.

deleterOfWorlds
  • 552
  • 5
  • 9
  • This worked, thanks. However jaydee3's answer also worked and he was a tad faster. And i can only accept one answer, but i'll +1 you! Thanks! – Jens Bergvall Mar 06 '12 at 15:29