NSNumberFormatter:大量显示'k'而不是',000'吗? [英] NSNumberFormatter : Show 'k' instead of ',000' in large numbers?

查看:149
本文介绍了NSNumberFormatter:大量显示'k'而不是',000'吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果可能的话,我想将我的大笔金额从100,000更改为$ 100,000.

I'd like to change my large numbers from 100,000 to $100K if this is possible.

这是我到目前为止所拥有的:

let valueFormatter = NSNumberFormatter()
valueFormatter.locale = NSLocale.currentLocale()
valueFormatter.numberStyle = .CurrencyStyle
valueFormatter.maximumFractionDigits = 0

我的问题

使用NSNumberFormatter,我如何输出10万美元而不是10万美元?

My Question

Using NSNumberFormatter, how can I output $100K rather than $100,000?

我最初的问题:

这是我到目前为止所拥有的:

self.lineChartView.leftAxis.valueFormatter = NSNumberFormatter()
self.lineChartView.leftAxis.valueFormatter?.locale = NSLocale.currentLocale()
self.lineChartView.leftAxis.valueFormatter?.numberStyle = .CurrencyStyle
self.lineChartView.leftAxis.valueFormatter?.maximumFractionDigits = 0

翻译为:

let valueFormatter = NSNumberFormatter()
valueFormatter.locale = NSLocale.currentLocale()
valueFormatter.numberStyle = .CurrencyStyle
valueFormatter.maximumFractionDigits = 0

我的输出如下:

使用NSNumberFormatter,我如何输出10万美元而不是10万美元?

Using NSNumberFormatter, how can I output $100K rather than $100,000?

更新:

我想提供有关正在发生的事情的上下文,观看评论.

I wanted to provide context as to whats going on, watch comments.

func setDollarsData(months: [String], range: Double) {

    var dataSets: [LineChartDataSet] = [LineChartDataSet]()

    var yVals: [ChartDataEntry] = [ChartDataEntry]()
    for var i = 0; i < months.count; i++ {
        // I'm adding my values here in value:, value takes a Double
        yVals.append(ChartDataEntry(value: county[userFavs[0]]![i], xIndex: i))
    }

    let set1: LineChartDataSet = LineChartDataSet(yVals: yVals, label: self.userFavs[0])
    set1.axisDependency = .Left
                set1.setColor(UIColor.redColor().colorWithAlphaComponent(0.5))
     set1.setCircleColor(UIColor.redColor())
     set1.lineWidth = 2.0
     set1.circleRadius = 6.0
     set1.fillAlpha = 65 / 255.0

     dataSets.append(set1)

    let data: LineChartData = LineChartData(xVals: months, dataSets: dataSets)
    data.setValueTextColor(UIColor.whiteColor())

    // this is where I set the number formatter
    self.lineChartView.gridBackgroundColor = UIColor.darkGrayColor()
    self.lineChartView.leftAxis.startAtZeroEnabled = false
    self.lineChartView.leftAxis.valueFormatter = NSNumberFormatter()
    self.lineChartView.leftAxis.valueFormatter?.locale = NSLocale.currentLocale()
    self.lineChartView.leftAxis.valueFormatter?.numberStyle = .CurrencyStyle
    self.lineChartView.leftAxis.valueFormatter?.maximumFractionDigits = 0

    // set it to the chart // END OF THE LINE
    self.lineChartView.data = data // outputs to my chart

    }

如您所见,一旦将数字转储到yVals中,我将失去对它们的访问权限,因此,这些扩展仅在我侵入框架后才有效.

As you can see, once I dump the numbers into yVals, I lose access to them so those extensions will only work if I hack into the framework.

推荐答案

编辑/更新

迅速3或更高版本

extension FloatingPoint {
    var kFormatted: String {
        return String(format: self >= 1000 ? "$%.0fK" : "$%.0f", (self >= 1000 ? self/1000 : self) as! CVarArg )
    }
}

您可以像这样使用它来格式化输出:

The you can use it like this to format your output:

10.0.kFormatted     // "$10"
100.0.kFormatted    // "$100"
1000.0.kFormatted   // "$1K"
10000.0.kFormatted  // "$10K"
162000.0.kFormatted  // "$162K"
153000.0.kFormatted  // "$153K"
144000.0.kFormatted  // "$144K"
135000.0.kFormatted  // "$135K"
126000.0.kFormatted  // "$126K"

这篇关于NSNumberFormatter:大量显示'k'而不是',000'吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆