I convert numbers to pure English words, and I came across some very strange situations: NSNumberFormatter has a strange conclusion, less than the desired result, but the number taken as a parameter does not cause overflow.
I have the following code:
import Foundation var numberFormatter: NumberFormatter = NumberFormatter() numberFormatter.numberStyle = .spellOut var result: String? result = numberFormatter.string(from: 999999999999999999) print(result ?? "nil")
and that prints eighteen quadrillion fourteen trillion three hundred ninety-eight billion five hundred nine million four hundred eighty-one thousand nine hundred eighty-four , which is equivalent to 18014398509481984 < 999999999999999999 . If I try to get the words from 18014398509481984 , the result will be the one I expected, the line described above. However, if I add another 9 to 999.. , it will work with the message:
integer literal 9999999999999999999 overflows when stored in Int
Here is the Swift Sandbox Test to make the question more clear.
My actual question: Assuming the conclusion of the first attempt: 180140398509481984 is some kind of limit for numberFormatter.string(from:) , why does 999999999999999999 not lead to overflow, but simply displays this limit and 9999999999999999999 (with an additional 9) leads to overflow?
source share