//通過NSNumberFormatter,同樣可以設置NSNumber輸出的格式。例如如下代碼: NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; formatter.numberStyle = NSNumberFor ...
//通過NSNumberFormatter,同樣可以設置NSNumber輸出的格式。例如如下代碼: NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; formatter.numberStyle = NSNumberFormatterDecimalStyle; NSString *string = [formatter stringFromNumber:[NSNumber numberWithInt:123456789]]; NSLog(@"Formatted number string:%@",string); //輸出結果為:[1223:403] Formatted number string:123,456,789 //其中NSNumberFormatter類有個屬性numberStyle,它是一個枚舉型,設置不同的值可以輸出不同的數字格式。該枚舉包括: typedef NS_ENUM(NSUInteger, NSNumberFormatterStyle) { NSNumberFormatterNoStyle = kCFNumberFormatterNoStyle, NSNumberFormatterDecimalStyle = kCFNumberFormatterDecimalStyle, NSNumberFormatterCurrencyStyle = kCFNumberFormatterCurrencyStyle, NSNumberFormatterPercentStyle = kCFNumberFormatterPercentStyle, NSNumberFormatterScientificStyle = kCFNumberFormatterScientificStyle, NSNumberFormatterSpellOutStyle = kCFNumberFormatterSpellOutStyle }; //各個枚舉對應輸出數字格式的效果如下:其中第三項和最後一項的輸出會根據系統設置的語言區域的不同而不同。 [1243:403] Formatted number string:123456789 [1243:403] Formatted number string:123,456,789 [1243:403] Formatted number string:¥123,456,789.00 [1243:403] Formatted number string:-539,222,988% [1243:403] Formatted number string:1.23456789E8 [1243:403] Formatted number string:一億二千三百四十五萬六千七百八十九