白开心

  IT博客 :: 首页 ::  :: 联系 :: 聚合  :: 管理 ::
  9 随笔 :: 76 文章 :: 28 评论 :: 0 Trackbacks
<html>
<head runat="server">
    
<title>数字金额转换为中文(Javascript版本)</title>

    
<script language="javascript" type="text/javascript">
        
/* 
        数字金额转换为中文,Create By HJ 2009-9-4
        
*/
        
var changeCNAMoney = function(money)
        {
            
//debugger;
            var IntNum,PointNum,IntValue,PointValue,unit,moneyCNY;
            
var Number  = "零一二三四五六七八九";
            
var NUMUnit = { LING : "",SHI : "",BAI : "",QIAN : "",WAN : "",YI : "亿" }
            
var CNYUnit = { YUAN : "",JIAO : "",FEN : "",ZHENG : "" };
            
var beforeReplace = 
            {
                Values :
                [
                    {Name: NUMUnit.LING 
+ NUMUnit.YI},               // 零亿
                    {Name: NUMUnit.LING + NUMUnit.WAN},              // 零万
                    {Name: NUMUnit.LING + NUMUnit.QIAN},             // 零千
                    {Name: NUMUnit.LING + NUMUnit.BAI},              // 零百
                    {Name: NUMUnit.LING + NUMUnit.SHI},              // 零十
                    {Name: NUMUnit.LING + NUMUnit.LING},             // 零零
                    {Name: NUMUnit.YI + NUMUnit.LING + NUMUnit.WAN}, // 亿零万
                    {Name: NUMUnit.LING + NUMUnit.YI},               // 零亿
                    {Name: NUMUnit.LING + NUMUnit.WAN},              // 零万
                    {Name: NUMUnit.LING + NUMUnit.LING}              // 零零
                ]
            };
            
var afterReplace = 
            {
                Values :
                [
                    {Name: NUMUnit.YI 
+ NUMUnit.LING}, //亿零
                    {Name: NUMUnit.WAN + NUMUnit.LING},//万零
                    {Name: NUMUnit.LING},              //
                    {Name: NUMUnit.LING},              //
                    {Name: NUMUnit.LING},              //
                    {Name: NUMUnit.LING},              //
                    {Name: NUMUnit.YI + NUMUnit.LING}, //亿零
                    {Name: NUMUnit.YI},                //亿
                    {Name: NUMUnit.WAN},               //
                    {Name: NUMUnit.LING}               //
                ]
            };
            
var pointBefore = 
            {
                Values :
                [
                    {Name: NUMUnit.LING 
+ CNYUnit.JIAO}, //零角
                    {Name: NUMUnit.LING + CNYUnit.FEN},  //零分
                    {Name: NUMUnit.LING + NUMUnit.LING}, //零零
                    {Name: CNYUnit.JIAO + NUMUnit.LING}  //角零
                ]
            };
            
var pointAfter = 
            {
                Values :
                [
                    {Name: NUMUnit.LING}, 
//
                    {Name: NUMUnit.LING}, //
                    {Name: ""},
                    {Name: CNYUnit.JIAO}  
//
                ]
            };
            
            
/// 递归替换
            var replaceAll = function(inputValue,beforeValue,afterValue)
            {
                
while(inputValue.indexOf(beforeValue) > -1)
                {
                    inputValue 
= inputValue.replace(beforeValue,afterValue);
                }
                
return inputValue;
            }
            
/// 获取输入金额的整数部分
            IntNum = money.indexOf("."> -1 ? money.substring(0,money.indexOf(".")) : money;
            
/// 获取输入金额的小数部分
            PointNum = money.indexOf("."> -1 ? money.substring(money.indexOf(".")+1) : "";
            IntValue 
= PointValue = "";
            
            
/// 计算整数部分
            for(var i=0;i<IntNum.length;i++)
            {
                
/// 获取数字单位
                switch((IntNum.length-i) % 8)
                {
                    
case 5:
                        unit 
= NUMUnit.WAN; //
                        break;
                    
case 0:
                    
case 4:
                        unit 
= NUMUnit.QIAN; //
                        break;
                    
case 7:
                    
case 3:
                        unit 
= NUMUnit.BAI; //
                        break;
                    
case 6:
                    
case 2:
                        unit 
= NUMUnit.SHI; //
                        break;
                    
case 1:
                        
if((IntNum.length-i) > 8)
                        {
                            unit 
= NUMUnit.YI; //亿    
                        }
                        
else { unit = ""; }
                        
break;
                    
default:
                        unit 
= "";
                        
break;
                }
                
/// 组成整数部分
                IntValue += Number.substr(parseInt(IntNum.substr(i,1)),1+ unit;
            }
            
            
/// 替换零
            for(var i=0;i<beforeReplace.Values.length;i++)
            {
                IntValue 
= replaceAll(IntValue,beforeReplace.Values[i].Name,afterReplace.Values[i].Name);             
            }
            
// 末尾是零则去除
            if(IntValue.substr(IntValue.length-1,1== NUMUnit.LING) IntValue = IntValue.substring(0,IntValue.length-1);
            
// 一十开头的替换为十开头
            if(IntValue.substr(0,2== Number.substr(1,1+ NUMUnit.SHI) IntValue = IntValue.substr(1,IntValue.length-1);
            
            
/// 计算小数部分
            if(PointNum != "")
            {
                PointValue 
= Number.substr(PointNum.substr(0,1),1+ CNYUnit.JIAO;
                PointValue 
+= Number.substr(PointNum.substr(1,1),1+ CNYUnit.FEN;
                
for(var i=0;i<pointBefore.Values.length;i++)
                {
                    PointValue 
= replaceAll(PointValue,pointBefore.Values[i].Name, pointAfter.Values[i].Name);
                }
            }
            moneyCNY 
= PointValue == "" ? IntValue + CNYUnit.YUAN + CNYUnit.ZHENG : IntValue + CNYUnit.YUAN + PointValue;
            
return moneyCNY;
        }
    
</script>
    
</head>
<body>
    
<div style="text-align: center">
        
<div id="divMsg">
        
</div>
        
<input type="text" value="10001002.53" id="txtNumber" />
        
<input type="button" id="btnChangeMoney" value="转换为中文" onclick="document.getElementById('divMsg').innerText = changeCNAMoney(document.getElementById('txtNumber').value);" />
    
</div>
</body>
</html>

posted on 2009-09-04 11:54 白开心 阅读(1285) 评论(0)  编辑 收藏 引用 所属分类: JavaScript
只有注册用户登录后才能发表评论。