How to convert int to NSString?

后端 未结 4 1106
不思量自难忘°
不思量自难忘° 2020-11-27 12:42

I\'d like to convert an int to a NSString in Objective C.

How can I do this?

相关标签:
4条回答
  • 2020-11-27 12:52
    NSString *string = [NSString stringWithFormat:@"%d", theinteger];
    
    0 讨论(0)
  • 2020-11-27 13:01

    If this string is for presentation to the end user, you should use NSNumberFormatter. This will add thousands separators, and will honor the localization settings for the user:

    NSInteger n = 10000;
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.numberStyle = NSNumberFormatterDecimalStyle;
    NSString *string = [formatter stringFromNumber:@(n)];
    

    In the US, for example, that would create a string 10,000, but in Germany, that would be 10.000.

    0 讨论(0)
  • 2020-11-27 13:06

    Primitives can be converted to objects with @() expression. So the shortest way is to transform int to NSNumber and pick up string representation with stringValue method:

    NSString *strValue = [@(myInt) stringValue];
    

    or

    NSString *strValue = @(myInt).stringValue;
    
    0 讨论(0)
  • 2020-11-27 13:18
    int i = 25;
    NSString *myString = [NSString stringWithFormat:@"%d",i];
    

    This is one of many ways.

    0 讨论(0)
提交回复
热议问题