使用js進行數字大小寫轉換

來源:互聯網
上載者:User

標籤:

//判斷月租金輸入是否是數字
function SetDefaultValueInError() {
  var yzj = document.getElementById("ctl00_ContentPlaceHolder2_RadTextBox11_text");//擷取源
  var tmpV = "";
  if (yzj != null) {
    tmpV = yzj.value;
    if (isNaN(tmpV)) {
      tmpV = 0;
    }
  }
  document.getElementById("ctl00_ContentPlaceHolder2_RadTextBox11_text").value = tmpV;//設定控制項的值(小寫)
  GetValueA(tmpV);
}

//擷取各個位上的數

function GetValueA(tmpV) {
  var yjz = MoneyCn(parseFloat(tmpV));
  var b, s, y, j;
  var a = "佰";
  var a1 = "拾";
  var a2 = "圓";
  var a3 = "角";
  var a4 = "分";
  b = yjz.toString().split(a.toString().split(‘‘)).length > 1 ? yjz.toString().split(a.toString().split(‘‘))                                                                                    [0].substring(yjz.toString().split(a.toString().split(‘‘))[0].length - 1) : "零";                                                                             

  s = yjz.toString().split(a1.toString().split(‘‘)).length > 1 ? yjz.toString().split(a1.toString().split(‘‘))                                                                                    [0].substring(yjz.toString().split(a1.toString().split(‘‘))[0].length - 1) : "零";
  y = yjz.toString().split(a2.toString().split(‘‘)).length > 1 ? yjz.toString().split(a2.toString().split(‘‘))                                                                              [0].substring(yjz.toString().split(a2.toString().split(‘‘))[0].length - 1) : "零";
  j = yjz.toString().split(a3.toString().split(‘‘)).length > 1 ? yjz.toString().split(a3.toString().split(‘‘))                                                                                  [0].substring(yjz.toString().split(a3.toString().split(‘‘))[0].length - 1) : "零";
  f = yjz.toString().split(a4.toString().split(‘‘)).length > 1 ? yjz.toString().split(a4.toString().split(‘‘))                                                                                  [0].substring(yjz.toString().split(a4.toString().split(‘‘))[0].length - 1) : "零";
  if (y == "拾" || y == "佰") {
    y = "零";
  }

  if (y == "一") {
    y = "壹";
  }

  //在這裡可以進行相應的賦值

  .......

  .......
}

//轉換大寫函數
function MoneyCn(number) {
  if (number == 0) {
  return "零";
  }
  var Result = ConvertNumberToCase(number);//轉換標準
//alert(Result);
  Result = Result.toString().replace("一", "壹");
  Result = Result.toString().replace("二", "貳");
  Result = Result.toString().replace("三", "三");
  Result = Result.toString().replace("四", "肆");
  Result = Result.toString().replace("五", "伍");
  Result = Result.toString().replace("六", "陸");
  Result = Result.toString().replace("七", "柒");
  Result = Result.toString().replace("八", "捌");
  Result = Result.toString().replace("九", "玖");
  Result = Result.toString().replace("九", "玖");
  Result = Result.toString().replace("十", "拾");
  Result = Result.toString().replace("百", "佰");
  Result = Result.toString().replace("千", "仟");
  //alert(Result);
  Result = Result.toString().replace("一", "壹");
  Result = Result.toString().replace("二", "貳");
  Result = Result.toString().replace("三", "三");
  Result = Result.toString().replace("四", "肆");
  Result = Result.toString().replace("五", "伍");  
  Result = Result.toString().replace("六", "陸");
  Result = Result.toString().replace("七", "柒");
  Result = Result.toString().replace("八", "捌");  
  Result = Result.toString().replace("九", "玖");
  Result = Result.toString().replace("九", "玖");
  Result = Result.toString().replace("十", "拾");
  Result = Result.toString().replace("百", "佰");
  Result = Result.toString().replace("千", "仟");
  Result = Result.toString().replace("一", "壹");
  if (Result.toString().split("點") != null && Result.toString().split("點").length >= 2) {
    var P = Result.toString().indexOf("點");
    //Result = Result.toString().push(P + 2, "角");
    Result = Result.toString().replace("點", "圓");
    Result = Result.toString().replace("角分", "角");
    Result = Result.toString().replace("零分", "");
    Result = Result.toString().replace("零角", "");
    Result = Result.toString().replace("分角", "");
    if (Result.toString().substring(0, 2) == "零圓") {
      Result = Result.Replace("零圓", "");
    }
  }
  else {
    Result += "圓整";
  }
  Result = "人民幣" + Result;
  return Result;
}

//轉換大寫標準
function ConvertNumberToCase(number) {
  var cPointCn = "點十百千萬十百千億十百千";
  var cNumberCn = "零一二三四五六七八九";
  if (number == "0") {
    return "" + cPointCn.substring(0, 1);
  }
  if (number.toString().split(‘.‘) != null && number.toString().split(‘.‘).length < 2) {
    number += ".";
  }
  var P = number.toString().indexOf(".");
  var Result = "";
  for (var i = 0; i < number.toString().length; i++) {
    if (P == i) {
      Result = Result.toString().replace("零十零", "零");
      Result = Result.toString().replace("零百零", "零");
      Result = Result.toString().replace("零千零", "零");
      Result = Result.toString().replace("零十", "零");
      Result = Result.toString().replace("零百", "零");
      Result = Result.toString().replace("零千", "零");
      Result = Result.toString().replace("零萬", "萬");
      Result = Result.toString().replace("零億", "億");
      Result = Result.toString().replace("億萬", "億");
      Result = Result.toString().replace("零點", "點");
    }else {

      if (P > i) {
        Result += "" + cNumberCn.substring(number.toString().substring(i, i + 1), parseInt(number.toString().substring(i, i + 1), 10) + 1) +       cPointCn.substring(P - i - 1, P - i);
      }
      else {
        var n = i - P;
          switch (n) {
          case 1:
            Result += "" + cNumberCn.substring(number.toString().substring(i, i + 1), parseInt(number.toString().substring(i, i + 1), 10) + 1) + "角";
          break;
          case 2:
            Result += "" + cNumberCn.substring(number.toString().substring(i, i + 1), parseInt(number.toString().substring(i, i + 1), 10) + 1) + "分";
          break;
          default:
            Result += "" + cNumberCn.substring(number.toString().substring(i, i + 1), parseInt(number.toString().substring(i, i + 1), 10) + 1);
          break;
        }
        Result = Result.toString().replace("零角", "角");
        Result = Result.toString().replace("零分", "分");
      }
    }
  }
  if (Result.toString().substring(Result.toString().Length - 1, 1) == "" + cPointCn.substring(0,1)) {
    Result = Result.toString().remove(Result.Length - 1); // 一點-> 一
  }
  if (Result.substring(0, 1) == cPointCn.substring(0, 1)) {
    Result = cNumberCn.substring(0, 1) + Result; // 點三-> 零點三
  }
  if ((Result.toString().Length > 1) && (Result.substring(1, 2) == cPointCn.substring(1, 2)) && (Result.substring(0, 1) ==               cNumberCn.substring(1, 2))) {
    Result = Result.toString().remove(0, 1); // 一十三-> 十三
  }
//alert(Result);
return Result;
}

使用js進行數字大小寫轉換

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.