Convert float value to NSString

后端 未结 5 623
感情败类
感情败类 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:33
    [NSString stringWithFormat:@"%f", tx_nb_demande_portabilite];
    
    0 讨论(0)
  • 2020-12-30 04:35

    one more option:

    NSString * str = [NSNumber numberWithFloat:value].stringValue;
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-30 04:43

    @"%f" sounds like more appropriate format string for float.

    0 讨论(0)
  • 2020-12-30 04:46

    A modern (and less verbose) approach would be:

    NSString *str = @(myFloat).description;
    
    0 讨论(0)
提交回复
热议问题