Reputation: 1508
How to convert an Array of NSNumber to Array of String in order to display the value in UITableView?
cell.textlabel.text = ?
Code:
var a = [68.208983, 6.373902, 1.34085, 3.974012, 110.484001,
61.380001, 1.325202, 0.8501030000000001, 0.8501030000000001,
0.8501030000000001, 3.647296, 1.28503]
Upvotes: 23
Views: 23348
Reputation: 468
Convert givent value in to String. If given value is nil then it will return empty string.
class func toString(_ anything: Any?) -> String {
if let any = anything {
if let num = any as? NSNumber {
return num.stringValue
} else if let str = any as? String {
return str
}
}
return ""
}
Just Copy paste this method convert to string without crash issue Thanks.
Upvotes: 0
Reputation: 56625
If you're going to display numeric data to the user, it's best to use a number formatter. That way your output will adapt to the formatting that your users are used to and expect based on their locale. It also allows you to configure how the numbers are presented (number of fraction digits, significant digits, rounding, etc.) without having to modify the numbers. For example, if you want to format a number as a decimal with two fraction digits, you would configure the formatter like this:
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 2
Depending on the user's locale, the output (thousand separator, decimal separator, and even digits(!)) will vary:
formatter.locale = Locale(identifier: "en")
formatter.string(from: 12345.6789) // 12,345.68
formatter.string(from: 0.12345) // 0.12
formatter.locale = Locale(identifier: "sv")
formatter.string(from: 12345.6789) // 12 345,68
formatter.string(from: 0.12345) // 0,12
formatter.locale = Locale(identifier: "hi")
formatter.string(from: 12345.6789) // १२,३४५.६८
formatter.string(from: 0.12345) // ०.१२
formatter.locale = Locale(identifier: "ar")
formatter.string(from: 12345.6789) // ١٢٬٣٤٥٫٦٨
formatter.string(from: 0.12345) // ٠٫١٢
Other number styles can be used to format other types of numeric data like currency, percent, or ordinal numbers:
formatter.locale = Locale(identifier: "en")
formatter.numberStyle = .ordinal
formatter.string(from: 1) // 1st
formatter.string(from: 2) // 2nd
formatter.string(from: 3) // 3rd
formatter.string(from: 4) // 4th
Upvotes: 0
Reputation: 7585
From what you posted is an array of Double
if you don't annotate them explicitly. If the array you posted is as it is, then you need this:
let arrayOfDoubles = [68.208983, 6.373902, 1.34085, 3.974012, 110.484001, 61.380001, 1.325202, 0.8501030000000001, 0.8501030000000001, 0.8501030000000001, 3.647296, 1.28503]
let stringArrayOfDoubles = arrayOfDoubles.map { String($0) }
Or, if you explicitly annotate the type as [NSNumber]
then you will need this:
let arrayOfNumbers: [NSNumber] = [68.208983, 6.373902, 1.34085, 3.974012, 110.484001, 61.380001, 1.325202, 0.8501030000000001, 0.8501030000000001, 0.8501030000000001, 3.647296, 1.28503]
let stringArrayOfNumbers = arrayOfNumbers.map { $0.stringValue }
Upvotes: 5