• JS实现金额转换(将输入的阿拉伯数字)转换成中文


    做项目的时候遇到的,就扒下来,收集了,希望对大家有用

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <script type="text/javascript" language="javascript">
    function clickchange(obj){
    var aaval=obj.value;
    alert(aaval);
    document.getElementById("bb").value=ChinaCost(aaval);
    }

    function ChinaCost(numberValue){
    var numberValue=new String(Math.round(numberValue*100)); // 数字金额
    var chineseValue=""; // 转换后的汉字金额
    var String1 = "零壹贰叁肆伍陆柒捌玖"; // 汉字数字
    var String2 = "万仟佰拾亿仟佰拾万仟佰拾元角分"; // 对应单位
    var len=numberValue.length; // numberValue 的字符串长度
    var Ch1; // 数字的汉语读法
    var Ch2; // 数字位的汉字读法
    var nZero=0; // 用来计算连续的零值的个数
    var String3; // 指定位置的数值
    if(len>15){
    alert("超出计算范围");
    return "";
    }
    if (numberValue==0){
    chineseValue = "零元整";
    return chineseValue;
    }

    String2 = String2.substr(String2.length-len, len); // 取出对应位数的STRING2的值
    for(var i=0; i<len; i++){
    String3 = parseInt(numberValue.substr(i, 1),10); // 取出需转换的某一位的值
    if ( i != (len - 3) && i != (len - 7) && i != (len - 11) && i !=(len - 15) ){
    if ( String3 == 0 ){
    Ch1 = "";
    Ch2 = "";
    nZero = nZero + 1;
    }
    else if ( String3 != 0 && nZero != 0 ){
    Ch1 = "零" + String1.substr(String3, 1);
    Ch2 = String2.substr(i, 1);
    nZero = 0;
    }
    else{
    Ch1 = String1.substr(String3, 1);
    Ch2 = String2.substr(i, 1);
    nZero = 0;
    }
    }
    else{ // 该位是万亿,亿,万,元位等关键位
    if( String3 != 0 && nZero != 0 ){
    Ch1 = "零" + String1.substr(String3, 1);
    Ch2 = String2.substr(i, 1);
    nZero = 0;
    }
    else if ( String3 != 0 && nZero == 0 ){
    Ch1 = String1.substr(String3, 1);
    Ch2 = String2.substr(i, 1);
    nZero = 0;
    }
    else if( String3 == 0 && nZero >= 3 ){
    Ch1 = "";
    Ch2 = "";
    nZero = nZero + 1;
    }
    else{
    Ch1 = "";
    Ch2 = String2.substr(i, 1);
    nZero = nZero + 1;
    }
    if( i == (len - 11) || i == (len - 3)){ // 如果该位是亿位或元位,则必须写上
    Ch2 = String2.substr(i, 1);
    }
    }
    chineseValue = chineseValue + Ch1 + Ch2;
    }

    if ( String3 == 0 ){ // 最后一位(分)为0时,加上“整”
    chineseValue = chineseValue + "整";
    }

    return chineseValue;
    }
    </script>
    </head>

    <body>
    <input type="text" id="aa" onblur="clickchange(this)" />
    <input type="text" id="bb" />
    </body>
    </html>

  • 相关阅读:
    6.Spark streaming技术内幕 : Job动态生成原理与源码解析
    5.Spark Streaming流计算框架的运行流程源码分析2
    4.Spark Streaming事务处理
    2.Spark Streaming运行机制和架构
    1.Spark Streaming另类实验与 Spark Streaming本质解析
    3.spark streaming Job 架构和容错解析
    35.Spark系统运行内幕机制循环流程
    unity3d 扩展NGUI Tweener —— TweenFillAmount
    unity3d 赛车游戏——复位点检测
    unity3d CarWaypoints插件
  • 原文地址:https://www.cnblogs.com/yuxia/p/3347173.html
Copyright © 2020-2023  润新知