1

I am struggling to show the Indian and Euro Locale value in words. Please anyone help on this.

    var outputString = ""
    let valueString = "9,58,888.875"
    let valueFormatter = NSNumberFormatter()
    let outputFormatter = NSNumberFormatter()
    outputFormatter.numberStyle = NSNumberFormatterStyle.DecimalStyle
    valueFormatter.numberStyle = NSNumberFormatterStyle.SpellOutStyle
    outputString = valueFormatter.stringFromNumber(outputFormatter.numberFromString(valueString as String)!)!
    print(outputString)
Mukesh N
  • 71
  • 1
  • 8

1 Answers1

0

I know it's very late. I have tried following solution, might not the best one but it's working for me. Hope it will be useful.

Note: - I have considered only till crore scale

@IBAction func startBtnClicked(sender: AnyObject) {

    if var amountStr = ammountTextField.text
    {
        let numberFormater = NSNumberFormatter()

        if amountStr.containsString(",")
        {
            amountStr = amountStr.stringByReplacingOccurrencesOfString(",", withString: "")
        }

        //eg: - amountStr = "45678432"

        if let amountNumber = numberFormater.numberFromString(amountStr)
        {
            //First get currency format
            let currencyFormatter = NSNumberFormatter()
            currencyFormatter.numberStyle = .CurrencyStyle
            currencyFormatter.currencySymbol = ""
            currencyFormatter.locale = NSLocale(localeIdentifier: "en_IN")

            let amountInCurrencyFormatStr = currencyFormatter.stringFromNumber(amountNumber)

            //amountInCurrencyFormatStr = "4,56,78,432"

            var outputStr = ""

            //Get each component in array 

            if let amountInCurrencyFormatComponentArray = amountInCurrencyFormatStr?.componentsSeparatedByString(",")
            {
                //[4,56,78,432]

                let scaleStrArr = ["thousand","lakh","crore"]

                let spellFormatter = NSNumberFormatter()
                spellFormatter.numberStyle = .SpellOutStyle

                var scaleStrCount = 0

                for var i = (amountInCurrencyFormatComponentArray.count - 1) ; i >= 0  ; i--
                {
                    //Iterate array, and append scale strings (["thousand","lakh","crore"]) at proper place

                    if let currencyStr = spellFormatter.stringFromNumber(numberFormater.numberFromString(amountInCurrencyFormatComponentArray[i])!)
                    {
                        if i == (amountInCurrencyFormatComponentArray.count - 1)
                        {
                            outputStr = currencyStr
                        }
                        else
                        {
                            if scaleStrCount < scaleStrArr.count
                            {
                                outputStr = "\(currencyStr) \(scaleStrArr[scaleStrCount]) \(outputStr)"

                                scaleStrCount++
                            }
                            else
                            {
                                outputStr = "\(currencyStr) \(outputStr)"
                            }

                        }
                    }
                    else
                    {
                        print("ERROR: - Unable to spell")
                    }

                }
            }
            else
            {
                print("ERROR: - No , in text field")
            }

            ammountTextField.text = amountInCurrencyFormatStr

            print("OUTPUT --> \(outputStr)")
            //OUTPUT -> "four crore fifty-six lakh seventy-eight thousand four hundred thirty-two"

        }
        else
        {
            print("ERROR: - No Number in text field")
        }


    }
    else
    {
        print("ERROR: - No value in text field")
    }
}
Chetan Koli
  • 178
  • 15