Convert float value to NSString

后端 未结 5 680
感情败类
感情败类 2020-12-30 04:28

Do you know how can i convert float value to nsstring value because with my code, there is an error.

My Code :

- (float)percent:(float)a :(float)b{
         


        
5条回答
  •  滥情空心
    2020-12-30 04:38

    You need to use %f format specifier for float, not %@.

    NSString *str = [NSString stringWithFormat:@"%f", myFloat];
    

    To use specific number of digits after decimal use %.nf where n is number of digits after decimal point.

    // 3 digits after decimal point
    NSString *str = [NSString stringWithFormat:@"%.3f", myFloat];
    

    Obj-C uses C printf style formatting. Please check printf man page for all other possible formatting.

提交回复
热议问题