有没有一种简单的方法可以在 iOS 中将货币格式化为字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5105227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a simple way to format currency into string in iOS?
提问by Hedin
I need a way to format the price from NSNumber into a string like this: "USD 0.99", not "$ 0.99".
我需要一种方法将价格从 NSNumber 格式化为这样的字符串:“USD 0.99”,而不是“$ 0.99”。
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.
所使用的方法对于 App Store 支持的任何货币都应该是绝对可行的。
回答by Matthias Bauch
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];
You haveto use the locale you get from the SKProduct. Don't use [NSLocale currentLocale]!
您必须使用从 SKProduct 获得的语言环境。不要使用 [NSLocale currentLocale]!
回答by Tieme
The – productsRequest:didReceiveResponse:
method gives you back a list of SKProducts.
该– productsRequest:didReceiveResponse:
方法会返回一个SKProducts列表。
Each product contains a property priceLocale
which contains the local currency of the product for the current user.
每个产品都包含一个属性priceLocale
,该属性包含当前用户的产品本地货币。
You could use the following sample code (apple's) to format it:
您可以使用以下示例代码 ( apple's) 对其进行格式化:
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[numberFormatter setLocale:product.priceLocale];
NSString *formattedString = [numberFormatter stringFromNumber:product.price];
Good luck!
祝你好运!
回答by Peter Kreinz
The Swift Example:
迅实施例:
var currencyFormatter = NSNumberFormatter()
currencyFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
currencyFormatter.locale = priceLocale //SKProduct->priceLocale
var currencyString = currencyFormatter.internationalCurrencySymbol
var format = currencyFormatter.positiveFormat
format = format.stringByReplacingOccurrencesOfString("¤", withString: currencyString)
currencyFormatter.positiveFormat = format
var formattedCurrency = currencyFormatter.stringFromNumber(price) //SKProduct->price
println("formattedCurrency: \(formattedCurrency)")//formattedCurrency: 0,89 EUR
回答by elbik
Nice example I found here http://bendodson.com/weblog/2014/12/10/skproduct-localized-price-in-swift/
我在这里找到了很好的例子http://bendodson.com/weblog/2014/12/10/skproduct-localized-price-in-swift/
import StoreKit
extension SKProduct {
@objc func localizedPrice() -> String {
let formatter = NSNumberFormatter()
formatter.numberStyle = .CurrencyStyle
formatter.locale = self.priceLocale
return formatter.stringFromNumber(self.price)!
}
}
回答by Ishu
use formatter in this way or you can also customize it
以这种方式使用格式化程序,或者您也可以自定义它
NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
or like this
或者像这样
[formatter setFormat:@"USD ###.00"];
i think you can check the currency for the country and store that in string and give that to the formatter.
我认为您可以检查该国家/地区的货币并将其存储在字符串中并将其提供给格式化程序。
回答by John Parker
The easiest way to achieve this would be to use the NSNumberFormatterclass to format the NSNumber value as required.
实现这一点的最简单方法是使用NSNumberFormatter类根据需要格式化 NSNumber 值。
Whilst the methods are too numerous to mention, this provides a wide variety of output formatting capabilities including the setInternationalCurrencySymbol:
method that should be of particular interest.
虽然方法多得无法提及,但它提供了多种输出格式化功能,包括setInternationalCurrencySymbol:
应该特别感兴趣的方法。
回答by Timothy Tripp
SKProduct price is a NSDecimalNumber object, so one way you could do this would be to extend the NSDecimalNumber class. Here's my Swift code for this:
SKProduct price 是一个 NSDecimalNumber 对象,所以你可以这样做的一种方法是扩展 NSDecimalNumber 类。这是我的 Swift 代码:
extension NSDecimalNumber {
func asCurrency(locale:NSLocale) -> String? {
var numberFormatter = NSNumberFormatter()
numberFormatter.formatterBehavior = NSNumberFormatterBehavior.Behavior10_4
numberFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
numberFormatter.locale = locale
return numberFormatter.stringFromNumber(self)
}
}
in Objective-C it would look like this:
在 Objective-C 中,它看起来像这样:
@interface NSDecimalNumber (CurrencyExtension)
- (NSString*) asCurrency: (NSLocale *)locale;
@end
@implementation NSDecimalNumber (DellExtensions)
- (NSString*) asCurrency: (NSLocale *)locale {
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[numberFormatter setLocale:product.priceLocale];
return [numberFormatter stringFromNumber:product.price];
}
@end