OMGPOP
OMGPOP

Reputation: 931

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

Upvotes: 3

Views: 1643

Answers (5)

devjme
devjme

Reputation: 720

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

Upvotes: 1

Joakim Danielson
Joakim Danielson

Reputation: 51831

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

Upvotes: 5

Reza Khonsari
Reza Khonsari

Reputation: 491

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

Upvotes: 0

slushy
slushy

Reputation: 12385

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.

Upvotes: 0

Rob Napier
Rob Napier

Reputation: 299265

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.)

Upvotes: 1

Related Questions