What is the best way to get a Swift string into a swift-currency/USD format? - swift

How do I get a string into a 'currency' format USD(xxx)?
I am trying to use the following library:
https://github.com/peek-travel/swift-currency
I want to be able to take a string:
var testStr1 = "$30.01"
var testStr2 = "$ 30.01"
and convert this into a currency as I have read from several posts using a double or float is bad, but if I start with a String, what else can I convert it to?
I thought I could use the "import Currency" library to do this, but this is not working.
let updatedString = testStr1.replacingOccurrences(of: "$", with: "")
let formatter = NumberFormatter()
formatter.locale = Locale.current // USA: Locale(identifier: "en_US")
formatter.numberStyle = .decimal
let number = formatter.number(from: test)
var dollars = USD(updatedString)
How do I get a string into a 'currency' format USD(xxx)? If there is a better way to accomplish this?

The basic concept presented by Himanshu works fine, but your problem isn't necessarily making use of an appropriate formatter, but how to fix your input, as the formatter expects a NSNumber and not a String.
So a quick internet check had me looking at Remove all non-numeric characters from a string in swift
So I could take a String, filter out all the "non numerical" junk and then make a Double out of it.
let input = Double(value.filter("0123456789.".contains))
from there I was able to borrow the concept from Himanshu and make a simple format function
func format(_ value: String, locale: Locale = Locale.current) -> String? {
guard let input = Double(value.filter("0123456789.".contains)) else { return nil }
//value.trimmingCharacters(in: .whitespacesAndNewlines)
let currencyFormatter = NumberFormatter()
currencyFormatter.usesGroupingSeparator = true
currencyFormatter.numberStyle = .currency
currencyFormatter.locale = locale
return currencyFormatter.string(from: NSNumber(value: input))
}
I then made use of a Playground to test it using
var testStr1 = "$30.01"
var testStr2 = "$ 30.01"
format(testStr1, locale: Locale(identifier: "en_US")) // $30.01
format(testStr2, locale: Locale(identifier: "en_US")) // $30.01
format(testStr1, locale: Locale(identifier: "fr_FR")) // 30,01 €
format(testStr2, locale: Locale(identifier: "fr_FR")) // 30,01 €
format(testStr1, locale: Locale(identifier: "de_DE")) // 30,01 €
format(testStr2, locale: Locale(identifier: "de_DE")) // 30,01 €
Now, if you specifically want to use USD(xxx) as the format, then you could simply use a basic NumberFormatter and generate your own String from the resulting conversion of the input to a Double
I have read from several posts using a double or float is bad
So, yes, maintaining a currency value as a Double or Float is generally a bad idea, currency values are typically maintained as a Int or Long, but this is due to how Double and Float representation works in computers, for the, general, presentation, you should be fine, but each use case needs be assessed.

let currencyFormatter = NumberFormatter()
currencyFormatter.usesGroupingSeparator = true
currencyFormatter.numberStyle = .currency
// 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: 9999.99)!
print(priceString) // Displays $9,999.99 in the US locale
**Forcing a Custom Locale**
You can override the users locale to display specific currency formats by changing the Locale using the identifier.
currencyFormatter.locale = Locale(identifier: "fr_FR")
if let priceString = currencyFormatter.string(from: 9999.99) {
print(priceString) // Displays 9 999,99 € in the French locale
}
currencyFormatter.locale = Locale(identifier: "de_DE")
if let priceString = currencyFormatter.string(from: 9999.99) {
print(priceString) // Displays 9.999,99 € in the German locale
}

Related

Swift lose precision in decimal formatting

I have an precision issue when dealing with currency input using Decimal type. The issue is with the formatter. This is the minimum reproducible code in playground:
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.isLenient = true
formatter.maximumFractionDigits = 2
formatter.generatesDecimalNumbers = true
let text = "89806.9"
let decimal = formatter.number(from: text)?.decimalValue ?? .zero
let string = "\(decimal)"
print(string)
It prints out 89806.89999999999 instead of 89806.9. However, most other numbers are fine (e.g. 8980.9). So I don't think this is a Double vs Decimal problem.
Edit:
The reason I need to use the formatter is that sometimes I need to deal with currency format input:
let text = "$89,806.9"
let decimal = formatter.number(from: text)?.decimalValue ?? .zero
print("\(decimal)") // prints 89806.89999999999
let text2 = "$89,806.9"
let decimal2 = Decimal(string: text2)
print("\(decimal2)") // prints nil
Using the new FormatStyle seems to generate the correct result
let format = Decimal.FormatStyle
.number
.precision(.fractionLength(0...2))
let text = "89806.9"
let value = try! format.parseStrategy.parse(text)
Below is an example parsing a currency using the currency code from the locale
let currencyFormat = Decimal.FormatStyle.Currency
.currency(code: Locale.current.currencyCode!)
.precision(.fractionLength(0...2))
let amount = try! currencyFormat.parseStrategy.parse(text)
Swedish example:
let text = "89806,9 kr"
print(amount)
89806.9
Another option is to use the new init for Decimal that takes a String and a FormatStyle.Currency (or a Number or Percent)
let amount = try Decimal(text, format: currencyFormat)
and to format this value we can use formatted(_:) on Decimal
print(amount.formatted(currencyFormat))
Output (still Swedish):
89 806,9 kr
I agree that this is a surprising bug, and I would open an Apple Feedback about it, but I would also highly recommend switching to Decimal(string:locale:) rather than a formatter, which will achieve your goal (except perhaps the isLenient part).
let x = Decimal(string: text)!
print("\(x)") // 89806.9
If you want to fix fraction digits, you can apply rounding pretty easily with * 100 / 100 conversions through Int. (I'll explain if it's not obvious how to do this; it works for Decimal, though not Double.)
Following Joakim Danielson Answer see this amazing documentation on the format style
Decimal(10.01).formatted(.number.precision(.fractionLength(1))) // 10.0 Decimal(10.01).formatted(.number.precision(.fractionLength(2))) // 10.01 Decimal(10.01).formatted(.number.precision(.fractionLength(3))) // 10.010
Amazingly detailed documentation
If this is strictly a rendering issue and you're just looking to translate a currency value from raw string to formatted string then just do that.
let formatter = NumberFormatter()
formatter.numberStyle = .currency
let raw = "89806.9"
if let double = Double(raw),
let currency = formatter.string(from: NSNumber(value: double)) {
print(currency) // $89,806.90
}
If there is math involved then before you get to the use of string formatters, I would point you to
Why not use Double or Float to represent currency? and
How to round a double to an int using Banker's Rounding in C as great starting points.
I get my response with double value and remove formatter.generatesDecimalNumbers line to get work.
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.isLenient = true
formatter.maximumFractionDigits = 2
//formatter.generatesDecimalNumbers = true // I removed this line
let text = "$89806.9"
let double = formatter.number(from: text)?.doubleValue ?? .zero // converting as double or float
let string = "\(double)"
print(string) // 89806.9
let anotherText = "$0.1"
let anotherDouble = formatter.number(from: anotherText)?.doubleValue ?? .zero // converting as double or float
let anotherString = "\(anotherDouble)"
print(anotherString) // 0.1

Avoiding non breaking space using NumberFormatter

I have a NumberFormatter setup like the following:
let currencyFormatter = NumberFormatter()
currencyFormatter.usesGroupingSeparator = true
currencyFormatter.groupingSeparator = "."
currencyFormatter.numberStyle = .currency
currencyFormatter.locale = Locale(identifier: "it_IT")
currencyFormatter.currencySymbol = ""
I need to retrieve a value based on a string value as input.
currencyFormatter.number(from: "1.000,00") // nil
currencyFormatter.number(from: "1.000,00\u{00a0}") // 1000
In the first sample, the formatter returns nil, while in the second I obtain the correct value (\u{00a0} is the non breaking space symbol).
Is there a way to make the first sample working without adding the space symbol?
If there is no currency symbol, this number formatter's style needs to be .decimal, not .currency. This is sufficient:
let currencyFormatter = NumberFormatter()
currencyFormatter.numberStyle = .decimal
currencyFormatter.locale = Locale(identifier: "it_IT")

NSNumberFormatter.number for currency format not working in Device but works in simulator

I've been trying to implement currency format based on passing my custom language identifier.
Below is my code
func currencyFormatter(language:String, amount:String) -> String {
let nsFormatter = NumberFormatter()
nsFormatter.numberStyle = .currency
nsFormatter.currencySymbol = ""
var formattedString: String?
var amountInNumber:NSNumber!
if let number = nsFormatter.number(from: amount)
{
amountInNumber = number.doubleValue as NSNumber
}
nsFormatter.locale = Locale(identifier: language)
formattedString = ((amountInNumber?.intValue) != nil) ? nsFormatter.string(from: amountInNumber) : amount
guard let finalString = formattedString else {
return ""
}
return finalString
}
I am trying to pass language as "fr-FR" and amount as "1234.45" then expecting output is "1 234,45".
This is working fine in simulator but not working in device (returning same value 1234.45)
Do i missed anything. Please help!
Thanks in advance
The decimal separator is locale-dependent, therefore parsing "1234.45"
fails if the locale's separator is not a period.
It the input string uses a fixed format with a period as decimal separator
then you can set the formatter's locale to "en_US_POSIX" for the conversion
from a string to a number. Then set it to the desired locale for the conversion
from number to a string.
Example:
func currencyFormatter(language: String, amount: String) -> String {
let nsFormatter = NumberFormatter()
nsFormatter.locale = Locale(identifier: "en_US_POSIX")
nsFormatter.numberStyle = .decimal
guard let number = nsFormatter.number(from: amount) else {
return amount
}
nsFormatter.locale = Locale(identifier: language)
nsFormatter.numberStyle = .currency
return nsFormatter.string(from: number) ?? amount
}
print(currencyFormatter(language: "fr-FR", amount: "1234.45"))
// 1 234,45 €

Make double 0.93 to integer 93

self.humidityLbl.text = "Humidity: \(self.currentWeather.humidity)%"
outputs Humidity: 0.93%. How can I convert double 0.93 to integer 93 in Swift?
Use a NumberFormatter with percent style to present a percentage
value to the user. This will use the proper format for the user's
locale. Examples:
let humidity = 0.93
let formatter = NumberFormatter()
formatter.numberStyle = .percent
formatter.locale = Locale(identifier: "en") // English
print(formatter.string(for: humidity)!) // 93%
formatter.locale = Locale(identifier: "de") // German
print(formatter.string(for: humidity)!) // 93 %
formatter.locale = Locale(identifier: "tr") // Turkish
print(formatter.string(for: humidity)!) // %93
formatter.locale = Locale(identifier: "ar") // Arabic
print(formatter.string(for: humidity)!) // ٩٣ ٪؜
Here I have set formatter.locale to different values for
demonstration purposes. If you don't assign a value, the user's
default settings are used.
Simple. Multiply by 100 and convert to an Int.
self.humidityLbl.text = "Humidity: \(Int(self.currentWeather.humidity * 100))%"
If you want to get fancy, create a helper extension on Double:
extension Double {
func percent() -> Int {
return Int(self * 100)
}
}
self.humidityLbl.text = "Humidity: \(self.currentWeather.humidity.percent())%"
You can cast to Int after multiplying it by 100, but you can use NSNumber instead:
let intNumber = NSNumber.init(value: 0.93 * 100).intValue
print(intNumber)
I think this is a safer way.

Elegant way of stripping string of currency characters?

Currently Im receiving a string from a service that contains currency characters (Ex. "$123.44", "123,44 €"). I would like to strip these stings of their currency characters in order to perform some calculations on these prices (adding money, subtracting money...etc).
I know I could replace occurrences of certain characters and then re-add them later after the calculations, but I get the impression that there might be a better solution out there.
Any ideas?
You can use NSNumberFormatter to convert a formatted price into a Double value
func getNumber(formattedPrice: String, localeID: String) -> Double? {
let formatter = NSNumberFormatter()
formatter.numberStyle = .CurrencyStyle
formatter.locale = NSLocale(localeIdentifier: localeID)
return formatter.numberFromString(formattedPrice)?.doubleValue
}
but you'll need to specify the locale
getNumber("$123.44", localeID: "en_US") // 123.44
getNumber("123,44€", localeID: "it_IT") // 123.44
getNumber("£123.44", localeID: "en_UK") // 123.44
You can use an NSCharacterSet to root out those characters, like this:
var dollarStr = "$1.50"
var yuanStr = "¥18.25"
var euroStr = "20.75 €"
let strArray = [dollarStr, yuanStr, euroStr]
let charSet = NSCharacterSet(charactersInString: "$¥€ ")
for str in strArray {
let trimmedStr = str.stringByTrimmingCharactersInSet(charSet)
print(trimmedStr)
}
prints out :
1.50
18.25
20.75