I have used below code successfully to format Int with thousand separators. However my current project required Int64 to be formatted the same way and it throws error 'Int64' is not convertible to 'NSNumber'
var numberFormatter: NSNumberFormatter {
let formattedNumber = NSNumberFormatter()
formattedNumber.numberStyle = .DecimalStyle
formattedNumber.maximumFractionDigits = 0
return formattedNumber
}
Sometimes it can be useful to add thousand separators (commas) to large numbers to make them easier to view. There is no simple way to do this in SQL for an int and bigint, but it can be achieved by converting to a money type first. The solution below gets the desired result :
We use the python string format syntax ' {:,.0f}'.format to add the thousand comma separators to the numbers. Then we use python’s map () function to iterate and apply the formatting to all the rows in the ‘Median Sales Price’ column.
Tip: Thousands and decimal separators can vary based on your local settings. For example; while dot (.) usually is a thousand separator in North America, comma (,) is a decimal separator in Europe. Below are some examples: Select the cells you want format.
If you would like to format numbers in thousands, you need to use thousands separator in the format code with a proper number placeholder. For example; 0, represents any number with the first thousands part hidden.
You mean when you call numberFormatter.stringFromNumber(12345678)
after the above code, like this?
let i64: Int64 = 1234567890
numberFormatter.stringFromNumber(i64)
Doesn’t look like Swift will cast from an Int64
to an NSNumber
:
let i = 1234567890
let n = i as NSNumber // OK
numberFormatter.stringFromNumber(i) // Also OK
// Compiler error: 'Int64' is not convertible to 'NSNumber'
let n64 = i64 as NSNumber
// so the implicit conversion will also fail:
numberFormatter.stringFromNumber(i64)
This is a bit confounding, since Swift Int
s are themselves usually the same size as Int64
s.
You can work around it by constructing an NSNumber
by hand:
let n64 = NSNumber(longLong: i64)
BTW beware that var
trick: it’s nice that it encapsulates all the relevant code for creating numberFormatter
, but that code will run afresh every time you use it. As an alternative you could do this:
let numberFormatter: NSNumberFormatter = {
let formattedNumber = NSNumberFormatter()
formattedNumber.numberStyle = .DecimalStyle
formattedNumber.maximumFractionDigits = 0
return formattedNumber
}()
If it’s a property in a struct/class, you could also make it a lazy var
which has the added benefit of only being running if the variable is used, like your var
, but only once.
struct Thing {
lazy var numberFormatter: NSNumberFormatter = {
println("blah")
let formattedNumber = NSNumberFormatter()
formattedNumber.numberStyle = .DecimalStyle
formattedNumber.maximumFractionDigits = 0
return formattedNumber
}()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With