1. 程式人生 > >JavaScript金額數字格式轉化成大寫格式

JavaScript金額數字格式轉化成大寫格式

gyyh.html

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="gyyh.js"></script>
<script language="javascript">
sl_alert(numToChinese("19876.30"));
sl_alert(numToChinese("19876"));
sl_alert(numToChinese("1000.00"));
</script>
</head>
<body>
<div
>
</div> </body> </html>

gyyh.js


function numToChinese(input)
{
    var s1 = "零壹貳叄肆伍陸柒捌玖";
    var s4 = "分角整元拾佰仟萬拾佰仟億拾佰仟";
    var temp = "";
    var result = "";
    var bZero = false;

    if (input == null)
        return "輸入字串不是數字串只能包括以下字元('0'~'9','.'),輸入字串最大隻能精確到仟億,小數點只能兩位!"
; var temp = trim(input); var f = parseFloat("0" + temp); var len = 0; if (temp.indexOf(".") == -1) len = temp.length; else len = temp.indexOf("."); if (len > s4.length - 3) return "輸入字串最大隻能精確到仟億,小數點只能兩位!"; var n1, n2 = 0; var num = ""; var unit = ""
; for (var i = 0; i < temp.length; i++) { if (i > len + 2) { break; } if (i == len) { continue; } n1 = parseInt(temp.charAt(i)); num = s1.substring(n1, n1 + 1); n2 = len - i + 2; unit = s4.substring(n2, n2 + 1); result = result + num + unit; } if ((len == temp.length) || (len == temp.length - 1)) result = result+ "整"; if (len == temp.length - 2) result = result + "零分"; result = trimZero(result); return result; } function trimZero(s) { var result = s, s1; var n1 = 0, n2 = 0; s1 = s; n1 = s1.indexOf("零仟零佰零拾零萬"); while (n1 > 0) { result = s1.substring(0, n1) + s1.substring(n1 + 8); s1 = result; n1 = s1.indexOf("零仟零佰零拾零萬"); } n1 = s1.indexOf("零拾"); while (n1 > 0) { result = s1.substring(0, n1) + s1.substring(n1 + 2); s1 = result; n1 = s1.indexOf("零拾"); } n1 = s1.indexOf("零佰"); while (n1 > 0) { result = s1.substring(0, n1) + s1.substring(n1 + 2); s1 = result; n1 = s1.indexOf("零佰"); } n1 = s1.indexOf("零仟"); while (n1 > 0) { result = s1.substring(0, n1) + s1.substring(n1 + 2); s1 = result; n1 = s1.indexOf("零仟"); } n1 = s1.indexOf("零元"); while (n1 > 0) { result = s1.substring(0, n1) + s1.substring(n1 + 1); s1 = result; n1 = s1.indexOf("零元"); } n1 = s1.indexOf("零角"); while (n1 > 0) { result = s1.substring(0, n1) + s1.substring(n1 + 2); s1 = result; n1 = s1.indexOf("零角"); } n1 = s1.indexOf("零分"); while (n1 > 0) { result = s1.substring(0, n1) + s1.substring(n1 + 2); s1 = result; n1 = s1.indexOf("零分"); } n1 = s1.indexOf("零萬"); while (n1 > 0) { if (s1.substring(0, n1).length > 0) result = s1.substring(0, n1) + s1.substring(n1 + 1); else result = s1.substring(0, n1) + s1.substring(n1 + 2); s1 = result; n1 = s1.indexOf("零萬"); } n1 = s1.indexOf("零億"); while (n1 > 0) { if (s1.substring(0, n1).length > 0) result = s1.substring(0, n1) + s1.substring(n1 + 1); else result = s1.substring(0, n1) + s1.substring(n1 + 2); s1 = result; n1 = s1.indexOf("零億"); } return result; } function trim(s) { while (s.substring(0,1) == ' ') { s = s.substring(1,s.length); } while (s.substring(s.length-1,s.length) == ' ') { s = s.substring(0,s.length-1); } return s; } function sl_alert(errinfo, url) { alert("系統提示:\n\n" + errinfo + "\n\n"); if (url != null) location.replace(url); }