使用js进行数字大小写的转换

//判断月租金输入是否是数字
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;
}

posted @ 2015-01-17 18:26  jamesmichel  阅读(2967)  评论(0编辑  收藏  举报