首页 > 代码库 > 数字类型钱币转换为大写

数字类型钱币转换为大写

做金融网站,免不了将数据转换为钱币的那种中文,今天做一个将算出来的数据在页面显示为大写的中午,还是繁体字那种,这给我纠结的!

好吧,先粘上我的劳动成果吧~

js代码,convertCurrency是一个方法,复制即可。

//小写金额转大写金额function convertCurrency(currencyDigits) {      var MAXIMUM_NUMBER = 99999999999.99;       var CN_ZERO = "零";      var CN_ONE = "壹";      var CN_TWO = "贰";      var CN_THREE = "叁";      var CN_FOUR = "肆";      var CN_FIVE = "伍";      var CN_SIX = "陆";      var CN_SEVEN = "柒";      var CN_EIGHT = "捌";      var CN_NINE = "玖";      var CN_TEN = "拾";      var CN_HUNDRED = "佰";      var CN_THOUSAND = "仟";      var CN_TEN_THOUSAND = "万";      var CN_HUNDRED_MILLION = "亿";      var CN_SYMBOL = "";      var CN_DOLLAR =(""+currencyDigits).indexOf(".")==-1? "":"元";     var CN_TEN_CENT = "角";      var CN_CENT = "分";      var CN_INTEGER = "";              var integral;       var decimal;     var outputCharacters;     var parts;      var digits, radices, bigRadices, decimals;      var zeroCount;      var i, p, d;      var quotient, modulus;            currencyDigits = currencyDigits.toString();             currencyDigits = currencyDigits.replace(/,/g, "");     currencyDigits = currencyDigits.replace(/^0+/, "");         if (Number(currencyDigits) > MAXIMUM_NUMBER) {        globalUtil.errorMsg(globalErrorMsg[‘100404‘]);        return "";      }            parts = currencyDigits.split(".");      if (parts.length > 1) {        integral = parts[0];        decimal = parts[1];        decimal = decimal.substr(0, 2);      }      else {        integral = parts[0];        decimal = "";      }      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);      outputCharacters = "";      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;      }      if (decimal != "") {        for (i = 0; i < decimal.length; i++) {         d = decimal.substr(i, 1);         if (d != "0") {          outputCharacters += digits[Number(d)] + decimals[i];         }        }      }      if (outputCharacters == "") {        outputCharacters = CN_ZERO + CN_DOLLAR;      }      if (decimal == "") {        outputCharacters += CN_INTEGER;      }      outputCharacters = CN_SYMBOL + outputCharacters;      return outputCharacters;  }

贾哥说:恭喜你,你完成了数据转换,马上快到6点了,你可以下班了,别忘记了写日报..............

哈哈,下班啦~

对了,记得自己在网上看的,说支持负数什么的,个人觉得干嘛是负数?不符合实际啊,不可以在文本框做一个onkeyup事件,不允许输入负数。

 

数字类型钱币转换为大写