How to big numbers YAxis format values convert to Indian Numbering Format like k(thousand), L(lakh), Cr(crore), Ar(Arab) and etc.on iOS barchart Swift

I have big numbers in values shown on YAxis. I would like to show currency in Indian Numbering Format like k(thousand), Lakh, Cr(crore), Ar(Arab) and etc.

value show in lakh & crore


My format changes according to value and cannot set different formatter for different values. For 1000 I would like to use 1K and 1000000 I would like to use 10L 

LargeValueFormatter: Can be used for formatting large values > "1.000". It will turn values like "1,000" into "1k", "10,00,000" will be "10L" (10 lakh), "1,00,00,00,000" will be "1Ar" (Arab) 

Value show in crore


Replace the below line code in ChartData or DataSet the object for formatting data values:

//value change

//                barChartView.leftAxis.valueFormatter = DefaultAxisValueFormatter(formatter: valFormatter)
        barChartView.leftAxis.valueFormatter = LargeValueFormatter()

Replace below source code in LargeValueFormatter.swift class

Value show in k(thousand)


/// Suffix to be appended after the values.
    ///
    // **default**: suffix: ["", "k", "m", "b", "t"]
      @objc open var suffix = ["", "k", "L", "Cr", "Ar"]    

fileprivate func format(value: Double) -> String
    {
        var sig = value
        //        var length = 0
        var length = 3
//        let maxLength = suffix.count - 1
        
        
        
        if (sig >= 10000000)  {
            sig = (sig / 10000000) //Cr
            length -= 0
        } else if (sig >= 100000) {
            sig = (sig / 100000) // ' Lac';
            length -= 1
        }
        else if(sig >= 1000){
            sig = (sig/1000) //' K'
            length -= 2
        }
        else{
            length -= 3  // ""
        }
        
        //old condition
        //        while sig >= 1000.0 && length < maxLength
        //        {
        //            sig /= 1000.0
        //            length += 1
        //        }
        
        var r = String(format: "%2.f", sig ) + suffix[length]
        
        if appendix != nil
        {
            r += appendix!
        }
        
        return r
    }

finally LargeValueFormatter.swift class


Related tutorials:

Post a Comment

0 Comments