iphone - 在格式化价格时获取 NSNumberFormatter 以拼出货币

标签 iphone ios ipad in-app-purchase

我现在正在我的应用中实现应用内购买。 为了获得本地化价格,我执行以下操作:

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
  [numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
  [numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
  [numberFormatter setLocale:self.priceLocale];
  NSString *formattedString = [numberFormatter stringFromNumber:self.price];
  [numberFormatter release];
  return formattedString;

这很好用。但是我想得到一个可以转换为 ASCII 字符串的字符串。那么有没有办法让数字格式化程序拼出货币。例如“0.99€”将变成“0.99 Eur”或“0.99 Euro”。

最佳答案

有一种方法可以通过在格式化程序的格式中替换 ¤ 来做到这一点:

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[numberFormatter setLocale:self.priceLocale];
NSString *currencyString = [numberFormatter internationalCurrencySymbol];
NSString *posFormat = [numberFormatter positiveFormat];
formatterFormat = [formatterFormat stringByReplacingOccurrencesOfString:@"¤" withString:currencyString];
[numberFormatter setPositiveFormat:posFormat];
NSString *formattedString = [numberFormatter stringFromNumber:self.price];
[numberFormatter release];
return formattedString;

关于iphone - 在格式化价格时获取 NSNumberFormatter 以拼出货币,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7057112/

相关文章:

iphone - 如何在 iOS 设备上浏览和播放本地音频文件?

html - -webkit-溢出-滚动 : touch kills overflow: hidden

iphone - 从observeValueForKeyPath 调用方法。

ios - HTTP 请求作为 iOS 中本地通知的后台任务

javascript - iPad 不会触发从垂直到水平的调整大小事件?

iphone - 检测 iPhone 应用程序的 iPad 2x 按钮

ios - 查找当前加速度计值

ios - 如何在用户在 Swift 中键入时调整 UITextField 的大小

iphone - 如何去除 UITabbaritem 图像的渐变效果和投影

iphone - 没有透明部分的图像蒙版