Eri*_*ric 4 objective-c nsnumberformatter ios
如何查看货币符号位于哪一侧?例如,在美国,该符号将是这样的:“ $56.58”,但在法国它会是这样的:“ 56.58\xe2\x82\xac”。那么我如何才能检测它是在右侧还是左侧呢?
NSNumberFormatter *currencyFormatter = [[[NSNumberFormatter alloc] init] autorelease];\n[currencyFormatter setLocale:[NSLocale currentLocale]];\nRun Code Online (Sandbox Code Playgroud)\n
如果您只想将数字格式化为货币,请将格式化程序设置numberStyle为NSNumberFormatterCurrencyStyle,然后使用其stringFromNumber:方法。
如果出于某种原因,您确实想知道格式化程序区域设置的格式中货币符号的位置,您可以向格式化程序询问positiveFormat并查找字符\xc2\xa4(U+00A4 CURRENCY SIGN)。
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];\nf.numberStyle = NSNumberFormatterCurrencyStyle;\n\nf.locale = [NSLocale localeWithLocaleIdentifier:@"en-US"];\nNSLog(@"%@ format=[%@] \xc2\xa4-index=%lu", f.locale.localeIdentifier, f.positiveFormat,\n (unsigned long)[f.positiveFormat rangeOfString:@"\\u00a4"].location);\n\nf.locale = [NSLocale localeWithLocaleIdentifier:@"fr-FR"];\nNSLog(@"%@ format=[%@] \xc2\xa4-index=%lu", f.locale.localeIdentifier, f.positiveFormat,\n (unsigned long)[f.positiveFormat rangeOfString:@"\\u00a4"].location);\n\nf.locale = [NSLocale localeWithLocaleIdentifier:@"fa-IR"];\nNSLog(@"%@ format=[%@] \xc2\xa4-index=%lu", f.locale.localeIdentifier, f.positiveFormat,\n (unsigned long)[f.positiveFormat rangeOfString:@"\\u00a4"].location);\nRun Code Online (Sandbox Code Playgroud)\n\n结果:
\n\n2015-06-10 21:27:09.807 commandline[88239:3716428] en-US format=[\xc2\xa4#,##0.00] \xc2\xa4-index=0\n2015-06-10 21:27:09.808 commandline[88239:3716428] fr-FR format=[#,##0.00\xc2\xa0\xc2\xa4] \xc2\xa4-index=9\n2015-06-10 21:27:09.808 commandline[88239:3716428] fa-IR format=[\xe2\x80\x8e\xc2\xa4#,##0] \xc2\xa4-index=1\nRun Code Online (Sandbox Code Playgroud)\n\n请注意,在这种fa-IR情况下,符号不是格式字符串中的第一个或最后一个字符。第一个字符(索引为零)是不可见的。这是 U+200E 从左到右的标记。