매우 늦었습니다. 나는 다음과 같은 해결책을 시도했지만, 최선의 해결책이 아닐지 모르지만 그것은 나를 위해 일하고있다. 희망이 유용 할 것입니다.
참고 : - I 만 억 루피 규모까지 고려했다
@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")
}
}