本文实例讲述了js实现格式化金额,字符,时间的方法。分享给大家供大家参考。具体实现方法如下:

复制代码 代码如下://金额转换成大写
     function toDaX(num){
        //金额大小写转换
           if (isNaN(num) || num > Math.pow(10, 12))
                    return "";
           var cn = "零壹贰叁肆伍陆柒捌玖";
           var unit = new Array("拾佰仟", "分角");
           var unit1 = new Array("万亿", "");
           if(parseFloat(num)==0||num==''){
               return "零圆";
           }
           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 "";
                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 "零").replace(/零+$/, "");
                   tmp1 = tmp1.replace(/^壹拾/, "拾");
                   tmp = (tmp1 + unit1[i].charAt(j - 1)) + tmp;
                }
               numArray[i] = tmp;
           }
           numArray[1] = numArray[1] "";
           numArray[0] = numArray[0] "元" : numArray[0],numArray[1] = numArray[1].replace(/^零+/, "");
           numArray[1] = numArray[1].match(/分/) "整";
           return numArray[0] + numArray[1];
     } 
/** 金额格式化 添加“,”分隔符 */

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*\.\d\d)\d*/,"$1");  
    money=money.replace(".",",");  
    var re=/(\d)(\d{3},)/;  
    while(re.test(money)){  
      money=money.replace(re,"$1,$2");  
    }  
    money=money.replace(/,(\d\d)$/,".$1"); 
    var money =  money_flag+""+money.replace(/^\./,"0.")
    return money;          
}

/** 金额格式化 去“,”分隔符 */
function delComma(value) {
  var rtnVal = value + "";
  return rtnVal.replace(/,/g,"");
}

/**
 * 金额格式化 字符串添加小数点
 */
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);
}
/**
 * 金额格式化 数字去小数点
 */
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+'%';
}

/** 字符格式化 添加 空格 分隔符 */
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;
}

/** 字符格式化 去除 空格 分隔符 */
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]);
}

希望本文所述对大家的javascript程序设计有所帮助。

标签:
js,格式化,金额,字符,时间,方法

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
评论“js实现格式化金额,字符,时间的方法”
暂无“js实现格式化金额,字符,时间的方法”评论...