Nicholas1024
Nicholas1024

Reputation: 890

NSString not loading double value

I'm not sure what's going on here. For some reason NSString seems unwilling to load in the double value d.

Here's my code:

-(NSString*)minuteFormat:(double) d{
    NSString* mystring;
    if(d >= 10){
        mystring = [NSString stringWithFormat:@"%d", d];
    }else{
        mystring = [NSString stringWithFormat:@"0%d",d];
    }
    return(mystring);


}

Regardless of the value of d, the only thing that's getting returned is 0 or 00. (And I'm sure d is getting inputted correctly, as I've used breakpoints to check.)

Could someone tell me what's going on?

Upvotes: 1

Views: 2501

Answers (3)

Michele
Michele

Reputation: 681

You should use %f (double) like this

mystring = [NSString stringWithFormat:@"%f", f];

or do a typecast of the value. For this you may use [yourString floatValue]

Upvotes: 0

Jeff Ames
Jeff Ames

Reputation: 2044

%d is for integers. You probably want %f. See String Format Specifiers

Upvotes: 11

yan
yan

Reputation: 20982

%d is a decimal integer format string. Try %lf for double.

Upvotes: 2

Related Questions