开发者

NSString not loading double value

开发者 https://www.devze.com 2023-02-11 17:44 出处:网络
I\'m not sure what\'s going on here. For some reason NSString seems unwilling to load in the double value d.

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'v开发者_如何学JAVAe used breakpoints to check.)

Could someone tell me what's going on?


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


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


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]

0

精彩评论

暂无评论...
验证码 换一张
取 消