JS数字金额转换为货币汉字形式

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <base href="<%=basePath%>">

        <title>My JSP 'index.jsp' starting page</title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
    </head>

    <body>
        This is my JSP page.
        <br>
        <script type="text/javascript">
        
        /** 数字金额大写转换(可以处理整数,小数,负数) */
        function digit_uppercase(n) 
        {
            var fraction = ['', ''];
            var digit = ['', '', '', '', '', '', '', '', '', ''];
            var unit = [ ['', '', '亿'], ['', '', '', '']  ];
            var head = n < 0? '': '';
            n = Math.abs(n);
        
            var s = '';
        
            for (var i = 0; i < fraction.length; i++) 
            {
                s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
            }
            s = s || '';
            n = Math.floor(n);
        
            for (var i = 0; i < unit[0].length && n > 0; i++) 
            {
                var p = '';
                for (var j = 0; j < unit[1].length && n > 0; j++) 
                {
                    p = digit[n % 10] + unit[1][j] + p;
                    n = Math.floor(n / 10);
                }
                s = p.replace(/(零.)*零$/, '').replace(/^$/, '')  + unit[0][i] + s;
            }
            return head + s.replace(/(零.)*零元/, '').replace(/(零.)+/g, '').replace(/^整$/, '零元整');
    }
    /** 整数测试数据 
    alert(digit_uppercase(0));          // 零元整
    alert(digit_uppercase(123));        // 壹佰贰拾叁元整
    alert(digit_uppercase(1000000));    // 壹佰万元整
    alert(digit_uppercase(100000001));  // 壹亿零壹元整
    alert(digit_uppercase(1000000000)); // 壹拾亿元整
    alert(digit_uppercase(1234567890)); // 壹拾贰亿叁仟肆佰伍拾陆万柒仟捌佰玖拾元整
    alert(digit_uppercase(1001100101)); // 壹拾亿零壹佰壹拾万零壹佰零壹元整
    alert(digit_uppercase(110101010));  // 壹亿壹仟零壹拾万壹仟零壹拾元整
    */
    
    /** 小数测试数据 
    alert(digit_uppercase(0.12));          // 壹角贰分
    alert(digit_uppercase(123.34));        // 壹佰贰拾叁元叁角肆分
    alert(digit_uppercase(1000000.56));    // 壹佰万元伍角陆分
    alert(digit_uppercase(100000001.78));  // 壹亿零壹元柒角捌分
    alert(digit_uppercase(1000000000.90)); // 壹拾亿元玖角
    alert(digit_uppercase(1234567890.03)); // 壹拾贰亿叁仟肆佰伍拾陆万柒仟捌佰玖拾元叁分
    alert(digit_uppercase(1001100101.00)); // 壹拾亿零壹佰壹拾万零壹佰零壹元整
    alert(digit_uppercase(110101010.10));  // 壹亿壹仟零壹拾万壹仟零壹拾元壹角
    */
    
    /** 负数(欠款)测试数据 
    alert(digit_uppercase(-0.12));          // 欠壹角贰分
    alert(digit_uppercase(-123.34));        // 欠壹佰贰拾叁元叁角肆分
    alert(digit_uppercase(-1000000.56));    // 欠壹佰万元伍角陆分
    alert(digit_uppercase(-100000001.78));  // 欠壹亿零壹元柒角捌分
    alert(digit_uppercase(-1000000000.90)); // 欠壹拾亿元玖角
    alert(digit_uppercase(-1234567890.03)); // 欠壹拾贰亿叁仟肆佰伍拾陆万柒仟捌佰玖拾元叁分
    alert(digit_uppercase(-1001100101.00)); // 欠壹拾亿零壹佰壹拾万零壹佰零壹元整
    alert(digit_uppercase(-110101010.10));  // 欠壹亿壹仟零壹拾万壹仟零壹拾元壹角
    */
    </script>

    </body>
</html>
原文地址:https://www.cnblogs.com/ruidongjun/p/3342844.html