When the NumberFormatter() is asked to output Decimal/NSDecimalNumber values with more than 15 fractional digits, the output is rounded (apparently to the precision of Double numbers). Example:
14 decimals: 3.14159265358979
15 decimals: 3.141592653589793
16 decimals: 3.1415926535897900
17 decimals: 3.14159265358979000
18 decimals: 3.141592653589790000
19 decimals: 3.1415926535897900000
20 decimals: 3.14159265358979000000
Note the trailing zeros which are not expected.
Note that Decimal
numbers support a precision of 38-decimals. Example: Decimal.pi.description
3.14159265358979323846264338327950288419
Code to reproduce the rounding problem:
import Foundation
var number = Decimal.pi
let formatter = NumberFormatter()
for width in 10 ... 38 {
formatter.maximumFractionDigits = width
formatter.minimumFractionDigits = width
print(String(width) + " decimals: " +
(formatter.string(from: number as NSDecimalNumber) ?? ""))
}
Apple feedback ID: FB9835108