Skip to content

Instantly share code, notes, and snippets.

@michaellindahl
Created September 7, 2020 02:24
Show Gist options
  • Save michaellindahl/73b5cb0e998940d974535f570f2d9cc5 to your computer and use it in GitHub Desktop.
Save michaellindahl/73b5cb0e998940d974535f570f2d9cc5 to your computer and use it in GitHub Desktop.
NumberFormatter produces invalid result with numbers close to UInt.max
import Foundation
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
func printStrings(_ int: UInt) {
let formatted = formatter.string(for: int)
print("String interpolation: \(int)")
print("Number Formatter: \(formatted)")
}
print("Using UInt.max")
printStrings(UInt.max)
print("\nUsing UInt.max - 100")
printStrings(UInt.max - 100)
print("\nUsing UInt.max / 10")
printStrings(UInt.max / 10)
print("\nUsing UInt.max / 100")
printStrings(UInt.max / 100)
print("\nUsing 18446744073709551615")
printStrings(18446744073709551615)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment