vue 小写转大写方法

好久好久没更新了啊,其实有好多可以写的, 但是不知道为啥不想写了

方法一:

// 输入的金额进行大写转换
function transformation(val) {
    var fraction = ["", ""];
    var digit = ["", "", "", "", "", "", "", "", "", ""];
    var unit = [
        ["", "", "亿"],
        ["", "", "", ""],
    ];
    var head = val < 0 ? "" : "";
    val = Math.abs(val);
    var s = "";
    for (var i = 0; i < fraction.length; i++) {  
        s += (
            digit[Math.floor(val * 100 * Math.pow(10, i)/10) % 10] + fraction[i]
        ).replace(/零./, "");
    }
    
    s = s || "";
    val = Math.floor(val);
    for (var i = 0; i < unit[0].length && val > 0; i++) {
        var p = "";
        for (var j = 0; j < unit[1].length && val > 0; j++) {
            p = digit[val % 10] + unit[1][j] + p;
            val = Math.floor(val / 10);
        }
        s = p.replace(/(零.)*零$/, "").replace(/^$/, "") + unit[0][i] + s;
    }
    return head + s.replace(/(零.)*零元/, "").replace(/(零.)+/g, "").replace(/^整$/, "零元");
}

打印输出:

方法二:

function transformation1(val) {  
var val=new String(Math.round(val*100)); // 数字金额
  var chineseValue="";          // 转换后的汉字金额
  var String1 = "零壹贰叁肆伍陆柒捌玖";       // 汉字数字
  var String2 = "万仟佰拾亿仟佰拾万仟佰拾元角分";     // 对应单位
  var len=val.length;         // val 的字符串长度
  var Ch1;             // 数字的汉语读法
  var Ch2;             // 数字位的汉字读法
  var nZero=0;            // 用来计算连续的零值的个数
  var String3;            // 指定位置的数值
  if(len>15){
   alert("超出计算范围");
   return "";
  }
  if (val==0){
   
   chineseValue = "零元整";
   return chineseValue;
   
  }
  
  String2 = String2.substr(String2.length-len, len);   // 取出对应位数的STRING2的值
  
  for(var i=0; i<len; i++){
   
   String3 = parseInt(val.substr(i, 1),10);   // 取出需转换的某一位的值
   //alert(String3);
   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;
 }

打印输出:

 注意:其实两个都一样的,一定要试试这个这个数字,哈哈哈哈哈因为有的会有精度计算问题,可以多试试

posted @ 2023-07-18 13:41  西贝小小凤  阅读(652)  评论(0编辑  收藏  举报