Swift之将阿拉伯数字转换为中文大写数字

func translationArabicNum(arabicNum:Double) -> String {
    let arabicNumStr = String(format: "%.2f", arabicNum)
    let rang = (arabicNumStr as NSString).range(of: ".")
    if rang.length == 0 {
        return (arabicNumStr as NSString).integerValue.cn
    }
    let integerNum = (arabicNumStr as NSString).substring(with: NSRange(location: 0, length: rang.location)) as NSString
    let doubleNum = (arabicNumStr as NSString).substring(with: NSRange(location: rang.location + 1, length: arabicNumStr.count - (rang.location + 1))) as NSString
    let intStr = (integerNum.integerValue).cn
    var doubleStr = (doubleNum.integerValue).cn
    if doubleNum.integerValue == 0 {
        return intStr + "元"
    }
    
    if doubleNum.integerValue < 20 && doubleNum.integerValue > 9 {
        doubleStr = doubleStr.replacingOccurrences(of: "拾", with: "壹")
    } else {
        if doubleNum.integerValue > 0 {
            let arr = ["拾", "佰", "仟", "万", "亿"]
            for d in arr {
                doubleStr = doubleStr.replacingOccurrences(of: d, with: "")
            }
        }
    }
    let first = doubleNum.substring(to: 1) as NSString
    if first.integerValue == 0 {
        doubleStr = doubleStr + "分"
    } else {
        let last = doubleNum.substring(from: 1) as NSString
        doubleStr.insert("角", at: doubleStr.index(doubleStr.startIndex, offsetBy: 1))
        if last.integerValue > 0 {
            doubleStr.insert("分", at: doubleStr.index(doubleStr.startIndex, offsetBy: 3))
        }
    }
    let result = intStr + "元" + doubleStr
    return result
}

extension Int {
    var cn: String {
        get {
            if self == 0 {
                return ""
            }
            let zhNumbers = ["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"]
            let units = ["", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾","佰","仟"]
            var cn = ""
            var currentNum = 0
            var beforeNum = 0
            let intLength = Int(floor(log10(Double(self))))
            for index in 0...intLength {
                currentNum = self/Int(pow(10.0,Double(index)))%10
                if index == 0{
                    if currentNum != 0 {
                        cn = zhNumbers[currentNum]
                        continue
                    }
                } else {
                    beforeNum = self/Int(pow(10.0,Double(index-1)))%10
                }
                if [1,2,3,5,6,7,9,10,11].contains(index) {
                    if currentNum == 1 && [1,5,9].contains(index) && index == intLength { // 处理一开头的含十单位
                        cn = units[index] + cn
                    } else if currentNum != 0 {
                        cn = zhNumbers[currentNum] + units[index] + cn
                    } else if beforeNum != 0 {
                        cn = zhNumbers[currentNum] + cn
                    }
                    continue
                }
                if [4,8,12].contains(index) {
                    cn = units[index] + cn
                    if (beforeNum != 0 && currentNum == 0) || currentNum != 0 {
                        cn = zhNumbers[currentNum] + cn
                    }
                }
            }
            return cn
        }
    }
}
  • 使用如下:
	self.chineseMoneyLabel?.text = translationArabicNum(arabicNum: (self.inputTF.text! as NSString).doubleValue) + "零角零分"
	
  • 结果展示:
	100689123
	转换后:
	壹亿零佰陆拾捌万玖仟壹佰贰拾叁元
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

╰つ栺尖篴夢ゞ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值