Created
September 7, 2020 02:24
-
-
Save michaellindahl/73b5cb0e998940d974535f570f2d9cc5 to your computer and use it in GitHub Desktop.
NumberFormatter produces invalid result with numbers close to UInt.max
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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