如何使用 NSNumberFormatter 只显示前两位数字?

How to show only the first two digits with NSNumberFormatter?

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

我创造 NSNumberFormatter 喜欢

 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

更新 我不能,因为图形需要数据,我以字节为单位测量它们以构建良好的时间表。如果我一开始转换成兆字节和字节等不同的值,那么704字节就比图表中的1.5兆多了

除了 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
}

为什么不将数字转换为 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)))