How to show only the first two digits with NSNumberFormatter?

I have an example number 1329094. I want to get only the first two digits that I do, see 13

I create NSNumberFormatter as

  func setReceivedChart(description: [String], value: [Double]) { var descriptionString: String! var formatterNumber: NSNumber! var chartDataEntryArray = [ChartDataEntry]() for item in 0..<description.count { let chartEntry = ChartDataEntry(value: value[item], xIndex: item) chartDataEntryArray.append(chartEntry) descriptionString = description[item] formatterNumber = value[item] } let dataSet = LineChartDataSet(yVals: chartDataEntryArray, label: "") dataSet.drawCircleHoleEnabled = false dataSet.drawCirclesEnabled = true // change dataSet.drawCubicEnabled = true dataSet.drawFilledEnabled = true dataSet.circleRadius = 3.0 // for iphone dataSet.drawValuesEnabled = true print(descriptionString) let numberFormatter = NSNumberFormatter() numberFormatter.numberStyle = .NoStyle numberFormatter.usesSignificantDigits = true numberFormatter.maximumSignificantDigits = 2 dataSet.valueFormatter = numberFormatter // set color 47 206 255 dataSet.fillColor = UIColor(red: 47/255, green: 206/255, blue: 255/255, alpha: 1.0) let chartData = LineChartData(xVals: description, dataSet: dataSet) receivedChartView.data = chartData } 

But I see the result of 1300000

UPDATE I can’t, because the data is needed for graphics, I measure them in bytes to build a good graph. If I initially transformed into different values, such as megabytes and bytes, 704 bytes will be more than 1.5 megabytes in the diagram

0
source share
2 answers

Why not convert the number to String , delete unnecessary digits and convert them back.

 let number = 1329094 let numberAsString = String(number) let subString = numberAsString.substringToIndex(numberAsString.startIndex.advancedBy(2)) let trimmedNumber = Int(subString) 

or

 let number = 1329094 let numberOfDigits = String(number).characters.count let trimmedNumber = number / Int(pow(10.0, Double(numberOfDigits - 2))) 
+2
source

I do not know the number of formatter that will scale the number (remove trailing zeros), with the exception of NSByteCountFormatter . Therefore, you will need to scale each data point in a loop before adding it to your chart array. Maybe something like this.

Note: It seems very risky to outline something using only non-zero, significant numbers. Charts 12, 120, 1200, 12000 and 120000000000000 will show the same values.

 func setReceivedChart(description: [String], value: [Double]) { let significantFigures = NSNumberFormatter() significantFigures.usesSignificantDigits = true significantFigures.maximumSignificantDigits = 2 var chartDataEntryArray = [ChartDataEntry]() for item in 0..<description.count { if let datum = significantFigures.stringFromNumber(value[item]) { datum.stringByReplacingOccurrencesOfString("0", withString: "", options: .LiteralSearch, range: nil) let chartEntry = ChartDataEntry(value: Double(datum), xIndex: item) chartDataEntryArray.append(chartEntry) } else { // handle error, keep charDataEntryArray in sync with description[] } } let dataSet = LineChartDataSet(yVals: chartDataEntryArray, label: "") dataSet.drawCircleHoleEnabled = false dataSet.drawCirclesEnabled = true // change dataSet.drawCubicEnabled = true dataSet.drawFilledEnabled = true dataSet.circleRadius = 3.0 // for iphone dataSet.drawValuesEnabled = true let numberFormatter = NSNumberFormatter() numberFormatter.numberStyle = .NoStyle dataSet.valueFormatter = numberFormatter // set color 47 206 255 dataSet.fillColor = UIColor(red: 47/255, green: 206/255, blue: 255/255, alpha: 1.0) let chartData = LineChartData(xVals: description, dataSet: dataSet) receivedChartView.data = chartData } 
0
source

Source: https://habr.com/ru/post/911518/


All Articles