Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format Int64 with thousand separators

Tags:

xcode

ios

swift

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
}
like image 363
Kashif Avatar asked Jan 01 '15 17:01

Kashif


People also ask

How to add thousand separators to a large number in SQL?

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 :

How to add thousand comma separators to the numbers in Python?

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.

What is the difference between thousands and decimal separators in Excel?

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.

How to format numbers in thousands in Python?

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.


1 Answers

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 Ints are themselves usually the same size as Int64s.

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
    }()
}
like image 107
Airspeed Velocity Avatar answered Nov 11 '22 08:11

Airspeed Velocity