Rounding issue NumberFormatter: String to Decimal

Originator:papailhau
Number:rdar://45429291 Date Originated:20.10.2018
Status:Open Resolved:
Product:iOS + SDK Product Version:
Classification:AVFoundation Reproducible:Always
 
Area:
AVFoundation

Summary:
When using a NumberFormatter to convert from String to Decimal, rounding issues happen.
I would hope there is no need to go to floating point type to do this conversion.

PS: Decimal(string: "8.38") works fine.

Steps to Reproduce:

private var decimalFormatter = NumberFormatter()

extension String {
    func asDecimal() -> Foundation.Decimal? {
        let number = decimalFormatter.number(from: self)
        return number?.decimalValue
    }
}

"8.38".asDecimal()

Expected Results:
"8.38".asDecimal() should be 8.38

Actual Results:
"8.38".asDecimal() is actually 8.380000000000001

Version/Build:
Xcode 9 and 10

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!