I am formatting my currency using locale fr_CA, I use below code to do
let formatter = NumberFormatter()
formatter.locale = Locale(identifier: "fr_CA")
formatter.numberStyle = .currency
if let formattedTipAmount = formatter.string(from: 33 as NSNumber) {
print(formattedTipAmount)
}
I am getting output as 33,00 $, but my expected output as 33,00 CAD
Do I need to manually replace last symbol($) with (CAD) or any other proper way of doing it. Kindly help me.
Do I need to manually replace last symbol($) with (CAD)…
Yes, you do.
let formatter = NumberFormatter()
formatter.locale = Locale(identifier: "fr_CA")
formatter.numberStyle = .currency
formatter.currencySymbol = "CAD"
if let formattedTipAmount = formatter.string(from: 33) {
print(formattedTipAmount)
}
Related
how to set NumberFormatter to return currency symbol not the iso one but local which is visible in iOS locale settings pane. Like for Polish currency I need to have “zł” symbol not the “PLN”.
I cannot find any way to get it from system as this cannot be hardcoded. IAP localized price also uses “zł” not “PLN”
I tried this way:
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.currencySymbol = Locale.current.currencySymbol
formatter.maximumFractionDigits = 2
let price = formatter.string(from: (offeringPrice / 12) as NSNumber) ?? ""
but whatever I try to use as currency symbol I always get in return "PLN"
If you set formatter.locale to pl_PL it works:
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.maximumFractionDigits = 2
formatter.currencyCode = "PLN"
let polandLocale = Locale(identifier: "pl_PL")
let usLocale = Locale(identifier: "en_US")
let offeringPrice = 50.0
let price = (offeringPrice / 12) as NSNumber
formatter.locale = usLocale
formatter.string(from: price) // "PLN 4.17"
formatter.locale = polandLocale
formatter.string(from: price) // "4,17 zł"
I've found this solution
func getCurrencySymbol(from currencyCode: String) -> String? {
let locale = NSLocale(localeIdentifier: currencyCode)
if locale.displayName(forKey: .currencySymbol, value: currencyCode) == currencyCode {
let newlocale = NSLocale(localeIdentifier: currencyCode.dropLast() + "_en")
return newlocale.displayName(forKey: .currencySymbol, value: currencyCode)
}
return locale.displayName(forKey: .currencySymbol, value: currencyCode)
}
original answer here: https://stackoverflow.com/a/49146857/1285959
Extenstion for converter Swift 4.2
extension Float {
var localeCurrency: String {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.locale = .current
return formatter.string(from: self as NSNumber)!
}
}
Accessing the value
let amount = 200.02
print("Amount Saved Value ",String(format:"%.2f", amountSaving. localeCurrency))
For me Its return 0.00!
Looks to me Extenstion Perfect when accessing it return 0.00! Why?
I have function with number formatter.
When user enter a number in textField it's formatting and shows with additional zeros.
Example: I entered "15.45". My result "15.4500000000".
How could I make work formatter to hide 0s when the number haven't any signs after?
Example: I entered "15.45". My result is "15.45 x 0.085" = "1.31325" (Not "1.313250000000").
let formatter = NumberFormatter()
formatter.locale = Locale.current
formatter.numberStyle = .decimal
formatter.minimumSignificantDigits = 10
if let text = textField.text, let number = formatter.number(from: text) {
year = number.doubleValue
yearLabel.text = formatter.string(from: NSDecimalNumber(value: year).multiplying(by: 1))
}
Try not setting minimumSignificantDigits to 10, just delete that line.
let formatter = NumberFormatter()
formatter.locale = Locale.current
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 10
if let number = formatter.number(from: "1.123456789") {
formatter.string(from: number)
}
Just change
formatter.minimumSignificantDigits = 10
to
formatter.maximumFractionDigits = 10
I am using following code to convert string to US currency. However, I could not figure out how to disable round up.
For example, if the string value is "0.000012" code converts it to "$0.00".
The extension I am using it is from this SO answer:
The way I use:
print(Formatter.currency.locale) // "en_US (current)\n"
print(priceUsdInt.currency) // "$1.99\n"
To control rounding (accept 6 digits after point in this case) add formatter.maximumFractionDigits = 6; to your Formatter extension
extension Formatter {
static let currency = NumberFormatter(style: .currency)
static let currencyUS: NumberFormatter = {
let formatter = NumberFormatter(style: .currency)
formatter.locale = Locale(identifier: "en_US")
formatter.maximumFractionDigits = 6;
return formatter
}()
static let currencyBR: NumberFormatter = {
let formatter = NumberFormatter(style: .currency)
formatter.locale = Locale(identifier: "pt_BR")
formatter.maximumFractionDigits = 6;
return formatter
}()
}
0.000012 is should NOT convert to 0.01 , that would be wrong.
If you want to set up a different format you can change the decimal points
String(format: "%0.3f", string)
I am getting price of an object in string and I want to formate it as currency.
I am using the following code:
var priceString : NSString = urlDict.objectForKey("price") as NSString
var formatter = NSNumberFormatter()
formatter.numberStyle = .CurrencyStyle
formatter.locale = NSLocale.currentLocale()
let priceNumber = formatter.numberFromString(priceString)
I am getting the data fine in priceString but priceNumber is nil.
And if I try this:
var priceNumber : NSNumber = urlDict.objectForKey("price") as NSNumber
then also the priceNumber is nil.
What approach should I take to achieve a formatted price from the string?
Thanks all for the suggestions:
I have combined above solutions to form a function:
func formatAsPrice(priceString: NSString) -> NSString {
let rsSymbol = "\u{20B9}" // The currency symbol for India
var priceStrTemp = priceString
priceStrTemp = rsSymbol + priceStrTemp
var formatter = NSNumberFormatter()
formatter.numberStyle = .CurrencyStyle
formatter.locale = NSLocale.currentLocale()
formatter.locale = NSLocale(localeIdentifier: "en_IN") //Explicit
formatter.secondaryGroupingSize = 2 // grouping as Indian currency style
let priceNumber = formatter.numberFromString(priceStrTemp)!
var finalPrice: NSString! = (rsSymbol + " \(priceNumber)") as NSString!
return finalPrice
}
The Formatter code is fine.
You have to pass correct Currency string with currency symbol
func format(amount: String) -> NSNumber? {
let formatter = NSNumberFormatter()
formatter.numberStyle = .CurrencyStyle
formatter.locale = NSLocale.currentLocale()
return formatter.numberFromString(amount)
}
format("NOK 12,00") // returns 12.00
format("NOK 12") // Also fine
format("$ 12") // Error, my local currency is NOK (Norwegian krone)
format("12,00") // Error, needs currency symbol in the string.
If you want to parse trying without currency sublime like this "12,00" that you should set currencySymbol to empty string. Like this
func format(amount: String) -> NSNumber? {
let formatter = NSNumberFormatter()
formatter.numberStyle = .CurrencyStyle
formatter.currencySymbol = ""
...
}
format("12,00") // Works fine now