NSNumberFormatter Currency Without Symbol?

佐手、 提交于 2019-12-05 10:26:19

问题


I am using NSNumberFormatter to get a currency value from a string and it works well.

I use this code to do so:

NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
    [nf setNumberStyle:NSNumberFormatterCurrencyStyle];
    NSString *price = [nf stringFromNumber:[NSNumber numberWithFloat:[textField.text floatValue]]];

However, it always gives me a currency symbol at the start of the string. Rather than doing it manually form my given string, can I not somehow have the formatter not give the string any currency symbol?


回答1:


Yes, after you set the style, you can tweak specific aspects:

NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
[nf setNumberStyle:NSNumberFormatterCurrencyStyle];
[nf setCurrencySymbol:@""]; // <-- this
NSDecimalNumber* number = [NSDecimalNumber decimalNumberWithString:[textField text]];
NSString *price = [nf stringFromNumber:number];

Just as some advice, you should probably use a number formatter to read the string value, especially if a user is entering it (as suggested by your code). In this case, if the user enters locale-specific formatting text, the generic -floatValue and -doubleValue type methods won't give you truncated numbers. Also, you should probably use -doubleValue to convert to a floating point number from user-entered text that's a currency. There's more information about this in the WWDC'12 developer session video on internationalization.

Edit: Used an NSDecimalNumber in the example code to represent the number the user enters. It's still not doing proper validation, but better than the original code. Thanks @Mark!




回答2:


With Swift 5, NumberFormatter has a property called currencySymbol. currencySymbol has the following declaration:

var currencySymbol: String! { get set }

The string used by the receiver as a local currency symbol.

Therefore, if required for your formatting style, you can set this property to an empty String.


The following Playground sample code shows how to set your currency formatting style with an empty symbol:

import Foundation

let amount = 12000

let formatter = NumberFormatter()
formatter.numberStyle = NumberFormatter.Style.currency
formatter.currencySymbol = ""
formatter.locale = Locale(identifier: "en_US") // set only if necessary

let result = formatter.string(for: amount)
print(String(describing: result)) // prints: Optional("12,000.00")



回答3:


as for Swift Language

let mymoney = 12000

let formatter = NSNumberFormatter()
formatter.numberStyle = .CurrencyStyle
formatter.currencySymbol = ""
formatter.locale = NSLocale.currentLocale()

let resultString = formatter.stringFromNumber(mymoney)!



回答4:


A little tweak to @Imanou Petit answer.

let myDouble = 9999.99
let currencyFormatter = NumberFormatter()
currencyFormatter.usesGroupingSeparator = true
currencyFormatter.numberStyle = .currency
currencyFormatter.currencySymbol = ""
// localize to your grouping and decimal separator
currencyFormatter.locale = Locale.current

// We'll force unwrap with the !, if you've got defined data you may need more error checking

let priceString = currencyFormatter.string(from: NSNumber(value: myDouble))!
print(priceString) // Displays 9,999.99 without the currency & not as Optional


来源:https://stackoverflow.com/questions/12522436/nsnumberformatter-currency-without-symbol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!