我需要一种方法将价格从 NSNumber 格式化为这样的字符串: “0.99 美元”,而不是“0.99 美元”。
我的游戏使用自定义字体,它们无法包含适用于所有可用 App Store 货币(例如 GBP)的符号。所以我认为最好回滚到货币的字符串表示。
使用的方法对于 App Store 支持的任何货币都应该是绝对可以的。
最佳答案
如果您希望它本地化(即货币在价格的正确一侧),则有点麻烦。
NSDecimalNumber *price = [NSDecimalNumber decimalNumberWithString:@"1.99"];
NSLocale *priceLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"de_DE"] autorelease]; // get the locale from your SKProduct
NSNumberFormatter *currencyFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[currencyFormatter setLocale:priceLocale];
NSString *currencyString = [currencyFormatter internationalCurrencySymbol]; // EUR, GBP, USD...
NSString *format = [currencyFormatter positiveFormat];
format = [format stringByReplacingOccurrencesOfString:@"¤" withString:currencyString];
// ¤ is a placeholder for the currency symbol
[currencyFormatter setPositiveFormat:format];
NSString *formattedCurrency = [currencyFormatter stringFromNumber:price];
您必须使用从 SKProduct 获得的语言环境。不要使用 [NSLocale currentLocale]!
关于iphone - 有没有一种简单的方法可以在 iOS 中将货币格式化为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5105227/