NumberFormat formatting exceeds 16 decimal places exception

Example1:

let num = NSDecimalNumber(string: "0.123456789012345678909")

let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.usesGroupingSeparator = true
formatter.maximumFractionDigits = 25
formatter.minimumFractionDigits = 25
formatter.minimumIntegerDigits = 1
let str = formatter.string(from: num) ?? ""
print(str)

output

"0.1234567890123460000000000"

Example2:

let num = NSDecimalNumber(string: "12323.123456789012345678909")

let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.usesGroupingSeparator = true
formatter.maximumFractionDigits = 25
formatter.minimumFractionDigits = 25
formatter.minimumIntegerDigits = 1
let str = formatter.string(from: num) ?? ""
print(str)

output

"12,323.1234567890000000000000000"

How to correctly format the contents of the above two inputs?

You thread title says “exception”, which suggests that you’re seeing a crash. However, your post suggests that there’s no crash, but rather you’re concerned about the results being truncated. Am I understanding that correctly?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

NumberFormat formatting exceeds 16 decimal places exception
 
 
Q