本文主要是介绍js阿拉伯数字完美转换成大写,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
啥都不说了;
直接上代码
<head><script type="text/javascript">// Firefox, Google Chrome, Opera, Safari, Internet Explorer from version 9function OnInput (event) {var n = event.target.value;if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n))return ;var unit = "千百拾亿千百拾万千百拾元角分", str = "";n += "00";var p = n.indexOf('.');if (p >= 0)n = n.substring(0, p) + n.substr(p+1, 2);unit = unit.substr(unit.length - n.length);for (var i=0; i < n.length; i++)str += '零壹贰叁肆伍陆柒捌玖'.charAt(n.charAt(i)) + unit.charAt(i);document.getElementById("gaibian").value = str.replace(/零(千|百|拾|角)/g, "零").replace(/(零)+/g, "零").replace(/零(万|亿|元)/g, "$1").replace(/(亿)万|壹(拾)/g, "$1$2").replace(/^元零?|零分/g, "").replace(/元$/g, "元整");}// Internet Explorerfunction OnPropChanged (event) {if (event.propertyName.toLowerCase () == "value") {var n = event.srcElement.value;if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n))return ;var unit = "千百拾亿千百拾万千百拾元角分", str = "";n += "00";var p = n.indexOf('.');if (p >= 0)n = n.substring(0, p) + n.substr(p+1, 2);unit = unit.substr(unit.length - n.length);for (var i=0; i < n.length; i++)str += '零壹贰叁肆伍陆柒捌玖'.charAt(n.charAt(i)) + unit.charAt(i);document.getElementById("gaibian").value = str.replace(/零(千|百|拾|角)/g, "零").replace(/(零)+/g, "零").replace(/零(万|亿|元)/g, "$1").replace(/(亿)万|壹(拾)/g, "$1$2").replace(/^元零?|零分/g, "").replace(/元$/g, "元整");}}</script>
</head>
<body>其他没有做校验,请输入数字:<input type="text" oninput="OnInput (event)" onpropertychange="OnPropChanged (event)"value="Text field" />转成大写后:<input type="text" id ="gaibian" value="Text field" />
</body>
这篇关于js阿拉伯数字完美转换成大写的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!