我知道這是很晚了。我試過下面的解決方案,可能不是最好的,但它對我有用。希望它會有用。
注: - 我只考慮,直到盧比規模
@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")
}
}