银行金额大写算法(支持负数)

function convertCurrency(currencyDigits,moneyType){
 var MAXIMUM_NUMBER = 99999999999.99;
  var CN_ZERO = "%u96F6";
  var CN_ONE = "%u58F9";
 var CN_TWO = "%u8D30";
 var CN_THREE = "%u53C1";
 var CN_FOUR = "%u8086";
 var CN_FIVE = "%u4F0D";
 var CN_SIX = "%u9646";
 var CN_SEVEN = "%u67D2";
  var CN_EIGHT = "%u634C";
  var CN_NINE = "%u7396";
  var CN_TEN = "%u62FE";
  var CN_HUNDRED = "%u4F70";
  var CN_THOUSAND = "%u4EDF";
  var CN_TEN_THOUSAND = "%u4E07";
  var CN_HUNDRED_MILLION = "%u4EBF";
  var CN_SYMBOL="";
 var CN_DOLLAR = "%u5143";
 var CN_TEN_CENT = "%u89D2";
 var CN_CENT = "%u5206";
  var CN_INTEGER = "%u6574";
  var integral; // Represent integral part of digit number.
 var decimal; // Represent decimal part of digit number.
 var outputCharacters; // The output result.
 var parts;
 var digits, radices, bigRadices, decimals;
 var zeroCount;
 var i, p, d;
 var quotient, modulus;
 //var CN_SYMBOL = moneyType;
 if (moneyType!="请选择一项" && moneyType!=""){
  CN_SYMBOL = moneyType
 }else{
  CN_SYMBOL = ""
 }
 // Validate input string:
  currencyDigits = currencyDigits.toString();
 if (currencyDigits == ""){
    return "";
  }
  /*
 if (currencyDigits.match(/[^,.\d]/) != null)
 {
   alert("错误:不能包含特殊字符!");
   return "";
  }
  if ((currencyDigits).match(/^((\d{1,3}(,\d{3})*(.((\d{3},)*\d{1,3}))?)|(\d+(.\d+)?))$/) == null)
 {
  alert("错误:不是纯数字!");
  return "";
 }
 */
 currencyDigits = currencyDigits.replace(/,/g, ""); // Remove comma delimiters.
 currencyDigits = currencyDigits.replace(/^0+/, ""); // Trim zeros at the beginning.
  if (Number(currencyDigits) > MAXIMUM_NUMBER) {
   alert("金额不能超过最大值:99999999999.99");
   return "";
  }
 var isNegative=false;
 if(currencyDigits.substring(0,1)=="-")
 {
  isNegative=true;
  currencyDigits=currencyDigits.substring(1,currencyDigits.lenght)
 }
   
  parts = currencyDigits.split(".");
  if (parts.length > 1){
    integral = parts[0];
     decimal = parts[1];
     // Cut down redundant decimal digits that are after the second.
    decimal = decimal.substr(0, 2);
 }else{
  integral = parts[0];
  decimal = "";
 }
 // Prepare the characters corresponding to the digits:
 digits = new Array(CN_ZERO, CN_ONE, CN_TWO, CN_THREE, CN_FOUR, CN_FIVE, CN_SIX, CN_SEVEN, CN_EIGHT, CN_NINE);
 radices = new Array("", CN_TEN, CN_HUNDRED, CN_THOUSAND);
 bigRadices = new Array("", CN_TEN_THOUSAND, CN_HUNDRED_MILLION);
 decimals = new Array(CN_TEN_CENT, CN_CENT);
 // Start processing:
 outputCharacters = "";
 // Process integral part if it is larger than 0:
 if (Number(integral) > 0) {
  zeroCount = 0;
  for (i = 0; i < integral.length; i++) {
   p = integral.length - i - 1;
   d = integral.substr(i, 1);
   quotient = p / 4;
   modulus = p % 4;
   if (d == "0"){
    zeroCount++;
   }else{
        if (zeroCount > 0){
          outputCharacters += digits[0];
        }
        zeroCount = 0;
        outputCharacters += digits[Number(d)] + radices[modulus];
      }
      if (modulus == 0 && zeroCount < 4) {
        outputCharacters += bigRadices[quotient];
     }
   }
    outputCharacters += CN_DOLLAR;
  }
   // Process decimal part if there is:
  if (decimal != "") {
     for (i = 0; i < decimal.length; i++){
      d = decimal.substr(i, 1);
      if (d != "0") {
        outputCharacters += digits[Number(d)] + decimals[i];
      }
    }
  }
   // Confirm and return the final output string:
  if (outputCharacters == "") {
    outputCharacters = CN_ZERO + CN_DOLLAR;
  }
  if (decimal == "") {
    outputCharacters += CN_INTEGER;
  }
 if(isNegative==true) 
 {
  outputCharacters = CN_SYMBOL + "负"+outputCharacters;
 }
 else
 {
   outputCharacters = CN_SYMBOL + outputCharacters;
 }
 
  return outputCharacters;
}

//----金额以千分位隔开
function transStr(str){
 var begin ="";
 var after ="";
 var l;
 var str2="";
 if(str.indexOf(".")<0){
  str=str+".00";
 }
 if(str.indexOf(".")>0){
    begin = str.substring(0,str.indexOf("."));
    after = str.substring(str.indexOf("."),str.length);
  }else{
    begin = str;
  }
 l=begin.length/3;
   if(l>1){
     for(var i=0;i<l;  ){
       str2=","+begin.substring(begin.length-3,begin.length)+str2;
       begin=begin.substring(0,begin.length-3);
       l=begin.length/3;
     }
     if(after.length<3){
       str2=begin+str2+after+"0";
     }else{
       str2=begin+str2+after
      }
     return str2.substring(1);
   }else{
     if(after.length<3){
       return str+"0";
     }else{
       return str;
      }
   }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值