人民币 数字 金额 转大写
一个html交互的小工具 实现了输入完数字金额后转换到大写的汉字金额
<input type="number" name="ActualRefundAmount" v-model="tmp.ActualRefundAmount" placeholder="实际退款金额" autocomplete="off" class="layui-input"> <input type="text" name="ActualRefundCapitalized" v-model="tmp.ActualRefundCapitalized" placeholder="实际退款大写金额" autocomplete="off" class="layui-input">
1 function DX(n) {//转大写 2 if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n)) return "数据非法"; 3 var unit = "千百拾亿千百拾万千百拾元角分", str = ""; 4 n += "00"; var p = n.indexOf('.'); 5 if (p >= 0) n = n.substring(0, p) + n.substr(p + 1, 2); 6 unit = unit.substr(unit.length - n.length); 7 for (var i = 0; i < n.length; i++) str += '零壹贰叁肆伍陆柒捌玖'.charAt(n.charAt(i)) + unit.charAt(i); 8 return str.replace(/零(千|百|拾|角)/g, "零").replace(/(零)+/g, "零").replace(/零(万|亿|元)/g, "$1").replace(/(亿)万|壹(拾)/g, "$1$2").replace(/^元零?|零分/g, "").replace(/元$/g, "元整"); 9 }
绑定input标签的失去焦点事件,也就是输入完之后:
1 $("#ActualRefundAmount").on("blur", function (e) { 2 if (e.delegateTarget.value == "") return; 3 //获取input输入的值 4 var dx = DX(e.delegateTarget.value); 5 if (dx == '数据非法') { 6 layer.msg("请输入数字!"); 7 $("#ActualRefundAmount").val(""); 11 return; 12 } 13 $("#ActualRefundCapitalized").val(dx); 16 });