I want to be able to convert any integer, I'm only using Int and UInt16 at the moment, into a String with a separator to group the thousands, if any. For example the value 12345 as input would output the string "12,345".
I found this solution but it fails, returning "12345".
import Foundation
func readable<T: BinaryInteger>(_ value: T) -> String {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
numberFormatter.groupingSeparator = Locale.current.groupingSeparator ?? ","
if let formattedValue = numberFormatter.string(from: NSNumber(nonretainedObject: value)) {
return formattedValue
} else {
return "\(value)"
}
}
I had first found a variation of the argument for NSNumber but that does not compile so I suspect that the argument for NSNumber is not correct.
How do I fix this?
NSNumber(value: value) // snippet does not compile.
Edit: The answer is found here https://stackoverflow.com/a/29999137/75062 but it didn't show on my searches ...