醉菜鸟 2021-11-25 09:00:10

JS实现将数字金额转换为大写人民币汉字

/**
 * JS实现将数字金额转换为大写人民币汉字
 * @param $number
 * @returns {string|*}
 */
const num_rmb = function($number) {
    const $rmb_num = ['零','壹','贰','叁','肆','伍','陆','柒','捌','玖'],
        $big_unit = ["","万","亿","兆"],
        $middle_unit = ["","拾","佰","仟"],
        $small_unit = ["角","分","毫","厘"];
    let $int, $float, $nums, rmb = '';
    if ($number === '') return '';
    if (($number = parseFloat($number)) >= 1e15)
        return alert('超出最大处理数字'),'';
    if ($number === 0)
        return $rmb_num[0] + "圆整";
    if (($number = $number.toString()).indexOf(".") === -1 ? ($int = $number,$float = "") : ($int = ($nums = $number.split("."))[0], $float = $nums[1].substr(0, 4)), parseInt($int, 10) > 0) {
        let $count = 0, $int_len = $int.length, $i = 0;
        for (; $i < $int_len; $i++) {
            let p = $int_len - $i - 1, h = p / 4, f = p % 4,$num = $int.substr($i, 1);
            $num === "0" ? $count++ : ($count > 0 && (rmb += $rmb_num[0]),$count = 0, rmb += $rmb_num[parseInt($num)] + $middle_unit[f]), f === 0 && $count < 4 && (rmb += $big_unit[h])
        }
        rmb += "圆"
    }
    if ($float !== '')
        for (let $float_let = $float.length, $i = 0; $i < $float_let; $i++) {
            const $num = $float.substr($i, 1);
            $num !== '0' && (rmb += $rmb_num[Number($num)] + $small_unit[$i])
        }
    return rmb === '' ? rmb += $rmb_num[0] + "圆整" : $float === '' && (rmb += "整"), rmb
}
意见反馈