PDA

View Full Version : conversion to doubleValue doesn’t work .. but to integerValue does




ulquiorra
Sep 25, 2009, 06:20 AM
Hello all I'm trying to convert a string to a double and it doesn't work. However if I convert it to an integer it does work

this is my code snippet

int myDuration = [myDurationString integerValue];
int conversionMinute = myDuration / 60;

if( myDuration < 60 )
{
[appDelegate.rep1 addObject:[NSString stringWithFormat:@"%d",myDuration]];
NSLog(@"show numbers %d", myDuration);
}
else
{
[appDelegate.rep1 addObject:[NSString stringWithFormat:@"%d",conversionMinute]];
NSLog(@"show numbers %d", conversionMinute);
}



Now if I try to do


double myDuration = [myDurationString doubleValue];
double conversionMinute = myDuration / 60;


then it doesn't work. It gives me an output of 0.

So the integerconversion works but somehow the double doesn't does anybody have an idea why?

ps. I already tried changing the format specifier to %f ... still no succes.



jnic
Sep 25, 2009, 08:43 AM
double conversionMinute = myDuration / 60;

then it doesn't work. It gives me an output of 0.


Try:
double conversionMinute = myDuration / 60.0;

Otherwise, you're dividing by an integer, which will cause a cast and be floored, resulting in a zero output in this case.

ulquiorra
Sep 28, 2009, 08:16 AM
thanks for your reply , the problem though has already been solved.

It did some other stuff a elsewhere in my code that kind of messed it up a little thanks though ;)