如何只显示与NSNumberFormatter的前两位数字?

我有一个例子1329094的数字。我只想得到我所看到的前两位数字13

我创buildNSNumberFormatter

  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 } 

但我看到结果1300000

更新我不能因为数据是需要的graphics,我测量它们的字节为build设一个很好的时间表。 如果我最初转换成不同的值,如兆字节和字节,图表中的704字节将超过1.5兆字节

为什么不把数字转换为String ,去掉不需要的数字并将其转换回来。

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

要么

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

我不知道一个数字格式化程序,将扩大数量(除去尾随零),除了NSByteCountFormatter 。 因此,在将其添加到图表数组之前,您需要缩放循环中的每个数据点。 也许这样的事情

注意:仅使用非零有意义的数字来绘制图表似乎非常危险。 图表12,120,1200,12000和120000000000000都将显示相同的值。

 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 }