conversion to doubleValue doesn’t work .. but to integerValue does

Discussion in 'iOS Programming' started by ulquiorra, Sep 25, 2009.

  1. ulquiorra macrumors member

    Joined:
    Jun 17, 2009
    #1
    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
    Code:
    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

    Code:
    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.
     
  2. jnic macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #2
    Try:
    Code:
    double conversionMinute = myDuration / 60[COLOR="Red"].0[/COLOR];
    Otherwise, you're dividing by an integer, which will cause a cast and be floored, resulting in a zero output in this case.
     
  3. ulquiorra thread starter macrumors member

    Joined:
    Jun 17, 2009
    #3
    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 ;)
     

Share This Page