有一个简单的方法来货币格式到iOS的字符串? [英] Is there a simple way to format currency into string in iOS?

查看:1202
本文介绍了有一个简单的方法来货币格式到iOS的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来从NSNumber的价格格式化成一个像这样的字符串:
0.99美元,而不是$ 0.99。

I need a way to format the price from NSNumber into a string like this: "USD 0.99", not "$ 0.99".

我的游戏中使用自定义字体,他们不能对所有可用的应用程序商店货币如英镑符号。所以我觉得这是更好地回滚到字符串重新币presentation。

My game uses custom fonts, and they could not have the symbols for all the available App Store currencies like GBP. So I think it's better to roll-back to string representation of currency.

所使用的方法应该是针对App Store的支持。

The method used should be absolutely OK for any currency that App Store supports.

推荐答案

如果你想让它本地化(对价格正确的一面,即货币),这是一个有点麻烦。

If you want it localized (ie the currency on the correct side of the price) it is a bit of a hassle.

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]!

You have to use the locale you get from the SKProduct. Don't use [NSLocale currentLocale]!

这篇关于有一个简单的方法来货币格式到iOS的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆