Home >Web Front-end >JS Tutorial >js method to format amounts, characters, and time_javascript skills

js method to format amounts, characters, and time_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:13:021505browse

The example in this article describes the js method of formatting amounts, characters, and time. Share it with everyone for your reference. The specific implementation method is as follows:

Copy code The code is as follows:
//Convert the amount to uppercase
Function toDaX(num){
//Amount case conversion
If (isNaN(num) || num > Math.pow(10, 12))
                     return "";
           var cn = "zero one two three four five Lu seven eight nine";
            var unit = new Array("十百千", "分角");
          var unit1 = new Array("Trillion", "");
If(parseFloat(num)==0||num==''){
Return "zero circle";
           }
          var numArray = num.toString().split(".");
          var start = new Array(numArray[0].length - 1, 2);
             function toChinese(num, index) {
              var num = num.replace(/d/g, function($1) {
Return cn.charAt($1) unit[index].charAt(start-- % 4 ? start % 4 : -1);
                    });
                   return num;
           }
for ( var i = 0; i < numArray.length; i ) {
              var tmp = "";
for ( var j = 0; j * 4 < numArray[i].length; j ) {
                var strIndex = numArray[i].length - (j 1) * 4;
                var str = numArray[i].substring(strIndex, strIndex 4);
                  var start = i ? 2: str.length - 1;
                  var tmp1 = toChinese(str, i);
                    tmp1 = tmp1.replace(/(zero.) /g, "zero").replace(/zero $/, "");
                                                                                                                                                        tmp1 = tmp1.replace(/^一十/, "十");
                      tmp = (tmp1 unit1[i].charAt(j - 1)) tmp;
                }
                 numArray[i] = tmp;
           }
​​​​​ numArray[1] = numArray[1] ? numArray[1] : "";
​​​​​​ numArray[0] = numArray[0] ? numArray[0] "元" : numArray[0],numArray[1] = numArray[1].replace(/^zero/, "");
​​​​​​ numArray[1] = numArray[1].match(/分/) ? numArray[1] : numArray[1] "whole";
              return numArray[0] numArray[1];
}  
/**Amount formatting Add "," separator*/

function addComma(money) {
if(money==""){
return "";
}
if(money){
money = money.trim();
}
    if(/[^0-9.- ]/.test(money)){  
    return money;
    }
    money = parseFloat(money) ""; 
    if('NaN' == money){
    return  "0.00";
    }
    var money_flag = "";
    if(money.indexOf("-") != -1){
    money = money.replace(/-/g,"");
    money_flag = "-";
    }

    money=money.replace(/^(d*)$/,"$1.");  
    money=(money "00").replace(/(d*.dd)d*/,"$1");  
    money=money.replace(".",",");  
    var re=/(d)(d{3},)/;  
    while(re.test(money)){  
      money=money.replace(re,"$1,$2");  
    }  
    money=money.replace(/,(dd)$/,".$1"); 
    var money =  money_flag "" money.replace(/^./,"0.")
    return money;          
}

/**Amount formatting removes the "," separator*/
function delComma(value) {
  var rtnVal = value "";
  return rtnVal.replace(/,/g,"");
}

/**
* Format amount, add decimal point to string
​*/
function addPoint(money){
if(/[^0-9.]/.test(money)){  
    return money;
    }
   
    if(money.length < 3 || money.indexOf(".") > -1){
    return money;
    }
   
    return money.substring(0,money.length - 2) "." money.substring(money.length - 2,money.length);
}
/**
* Formatting amounts and removing decimal points from numbers
​*/
function removePoint(money){
    if(/[^0-9.]/.test(money)){  
    return money;
    }
   
    var valueFloat = parseFloat(money) * 100;
    var valueInt = parseInt(valueFloat);
    return valueInt;
}
/* 格式化小数点后两位数字 以百分比显示 */
function addPercent(str){
    var percent = Math.floor(str * 100) / 100;
    percent=(percent.toFixed(2));
    return percent '%';
}

/**Character formatting Add space separator*/
function addSpace(value) {
if(value == null || value == ""){
return "";
}

    var value = value "";
    var tmpStr = "";
    while (value.length > 4) {
        tmpStr = tmpStr value.substring(0,4) " ";
        value = value.substring(4,value.length);
    }
    tmpStr = tmpStr value;
    return tmpStr;
}

/**Character formatting remove spaces separators*/
function removeSpace(value) {
var rtnVal = value "";
  return rtnVal.replace(/ /g,"");
}

// 格式化日期时间字符串
// YYYYMMDD-》YYYY-MM-DD
// YYYYMMDDhhmmss-》YYYY-MM-DD hh:mm:ss
function formatDatetime(oldvalue){
if(oldvalue == null){
return "";
}else if(oldvalue.length == 8){
return oldvalue.substring(0,4)
      "-" oldvalue.substring(4,6)
      "-" oldvalue.substring(6,8);
}else if(oldvalue.length == 14){
return oldvalue.substring(0,4)
      "-" oldvalue.substring(4,6)
      "-" oldvalue.substring(6,8)
     
      " " oldvalue.substring(8,10)
      ":" oldvalue.substring(10,12)
      ":" oldvalue.substring(12,14);
}else if(oldvalue.length == 6){
return oldvalue.substring(0,2)
      ":" oldvalue.substring(2,4)
      ":" oldvalue.substring(4,6);
}else{
return oldvalue;
}
}

function StringToDate(str){
var datainfo=str.split('-');
    return new Date(datainfo[0],datainfo[1],datainfo[2]);
}

I hope this article will be helpful to everyone’s JavaScript programming design.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn