ฉันต้องการแปลงเป็นintไฟล์NSStringใน Objective C
ฉันจะทำเช่นนี้ได้อย่างไร?
ฉันต้องการแปลงเป็นintไฟล์NSStringใน Objective C
ฉันจะทำเช่นนี้ได้อย่างไร?
คำตอบ:
Primitives สามารถแปลงเป็นวัตถุด้วย@()นิพจน์ ดังนั้นที่สั้นที่สุดวิธีคือการเปลี่ยนintไปNSNumberและรับแทนสายด้วยstringValueวิธีการ:
NSString *strValue = [@(myInt) stringValue];
หรือ
NSString *strValue = @(myInt).stringValue;
NSString *string = [NSString stringWithFormat:@"%d", theinteger];
int i = 25;
NSString *myString = [NSString stringWithFormat:@"%d",i];
นี่เป็นหนึ่งในหลาย ๆ วิธี
NSNumberFormatterถ้าสายนี้คือเพื่อนำเสนอต่อผู้ใช้ที่คุณควรใช้ สิ่งนี้จะเพิ่มตัวคั่นหลายพันตัวและจะเป็นไปตามการตั้งค่าการแปลสำหรับผู้ใช้:
NSInteger n = 10000;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
NSString *string = [formatter stringFromNumber:@(n)];
ตัวอย่างเช่นในสหรัฐอเมริกาสิ่งนั้นจะสร้างสตริง10,000แต่ในเยอรมนีจะเป็น10.000เช่นนั้น