ios - Changing grouping separator, currency symbol and position of currency symbol for multiple currencies in Swift -
my app uses multiple currencies, , these currencies uses different formats, example:
price ruble shows as: 1,101 Руб.
same amount us dollar shows as: us $1 101
how change grouping separator, currency symbol , position of currency symbol, defining set of different formats different currencies.
this how short code stands
var formatter = nsnumberformatter() formatter.numberstyle = .currencystyle formatter.locale = nslocale.currentlocale() formatter.stringfromnumber(4500000) //output : $4,500,000.00 //expected : 4,500,000 Руб.
try this:
struct number { static let formatterbelarusianruble: nsnumberformatter = { let formatter = nsnumberformatter() // set numberstyle .currencystyle formatter.numberstyle = .currencystyle // set desired negative , positive formats grouping, , currency symbol position formatter.positiveformat = "#,##0 ¤" formatter.negativeformat = "-#,##0 ¤" // set custom currency symbol formatter.currencysymbol = "Руб" return formatter }() } let stringtodisplay = number.formatterbelarusianruble.stringfromnumber(4500000) // 4,500,000 Руб
as extension:
extension double { struct number { static let formatterbyr: nsnumberformatter = { let formatter = nsnumberformatter() formatter.numberstyle = .currencystyle formatter.positiveformat = "#,##0 ¤" formatter.negativeformat = "-#,##0 ¤" formatter.currencysymbol = "Руб" return formatter }() static let formattereur: nsnumberformatter = { let formatter = nsnumberformatter() formatter.locale = nslocale(localeidentifier: "pt_pt") formatter.numberstyle = .currencystyle return formatter }() static let formatterusd: nsnumberformatter = { let formatter = nsnumberformatter() formatter.locale = nslocale(localeidentifier: "en_us") formatter.numberstyle = .currencystyle return formatter }() static let formatterrub: nsnumberformatter = { let formatter = nsnumberformatter() formatter.locale = nslocale(localeidentifier: "ru_ru") formatter.numberstyle = .currencystyle formatter.maximumfractiondigits = 0 return formatter }() static let formatterlocale: nsnumberformatter = { let formatter = nsnumberformatter() formatter.locale = nslocale.currentlocale() formatter.numberstyle = .currencystyle return formatter }() } var currencylocale: string { return number.formatterlocale.stringfromnumber(self) ?? "" } var currencyusd: string { return number.formatterusd.stringfromnumber(self) ?? "$0.00" } var currencyeur: string { return number.formattereur.stringfromnumber(self) ?? "0,00 €" } var currencybyr: string { return number.formatterbyr.stringfromnumber(self) ?? "0 pyб" } var currencyrub: string { return number.formatterrub.stringfromnumber(self) ?? "0,00 ₽" } }
usage
let amount = 4500000.0 let stringlocale = amount.currencylocale // "$4,500,000.00" let stringusd = amount.currencyusd // "$4,500,000.00" let stringeur = amount.currencyeur // "4 500 000,00 €" let stringbyr = amount.currencybyr // "4,500,000 Руб" let stringrub = amount.currencyrub // "4 500 000 ₽"
Comments
Post a Comment