iphone - 有没有一种简单的方法可以在 iOS 中将货币格式化为字符串?

标签 iphone objective-c ios app-store currency

我需要一种方法将价格从 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/

相关文章:

objective-c - 有没有办法检测用户点击触控板的右侧还是左侧?

ios - Firebase Analytics 持续跟踪 UINavigationController

iphone - 通过选择 UITableView 行调用新 View 错误

iphone - 应用程序必须准备好在 iTunes Connect 上上传,然后才能在 Xcode 中验证或提交

iphone - iOS 中带分页的 UICollectionView 的页数

iphone - UITableView 中的自定义 UIControl

ios - Objective C 到 Swift 代码转换,似乎无法在 Swift 中正确处理字节

ios - Swift-音频-按键声音不断出错

ios - 返回两种不同类型的 Swift 函数

iphone - 旋转后UIView图层阴影变暗