Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSNumberFormatter : Show 'k' instead of ',000' in large numbers?

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

This is what I have so far:

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

My Question

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


My original question:

This is what I have so far:

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

Which Translates to:

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

My output looks like this:

enter image description here

My Question

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


update:

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

    }

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.

like image 971
Dan Beaulieu Avatar asked Oct 19 '15 20:10

Dan Beaulieu


2 Answers

edit/update

Swift 3 or later

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"
like image 69
Leo Dabus Avatar answered Oct 15 '22 02:10

Leo Dabus


I've bumped into the same issue and solved it by implementing a custom formatter. Just started coding in Swift, so the code might not be the most idiomatic.

open class KNumberFormatter : NumberFormatter {
    override open func string(for obj: Any?) -> String? {
        if let num = obj as? NSNumber {
            let suffixes = ["", "k", "M", "B"]
            var idx = 0
            var d = num.doubleValue
            while idx < 4 && abs(d) >= 1000.0 {
                d /= 1000.0
                idx += 1
            }
            var currencyCode = ""
            if self.currencySymbol != nil {
                currencyCode = self.currencySymbol!
            }

            let numStr = String(format: "%.1f", d)

            return currencyCode + numStr + suffixes[idx]
        }
        return nil
    }
}
like image 22
f1024 Avatar answered Oct 15 '22 01:10

f1024