I am getting a string value that represents a price. I want to convert it to another string with specific precision and format it according to a current device Locale.
Decimal(string: "123,4567", locale: NSLocale.current)?.formatted(.number.precision(.fractionLength(precision)))
This code works for the german language and the output is "123,45". But if I switch to English the output is "123.00". The problem is with the dot instead of the comma. Any ideas how to fix it and show the correct number "123.45"?
2
Answers
The locale used for the input string must match the format of the string, so if it is a German format then use a German locale
This uses Locale.current (Swedish in this example) for the output
Since this is a price here is a currency example
If all you need is to format your decimal number to be displayed using a specific currency what you need is
Decimal.FormatStyle.Currency
. It will automatically discard the excess of precision length:edit/update:
I usually don’t like manipulating strings but if your source doesn’t have a specific format where sometimes de decimal separator is a comma and sometimes a period you need to replace your string comma with a period before converting it to Decimal: