---
title: "添加千分号"
author: "lanxing"
date: "2011-06-01T13:50:50Z"
tags: []
categories: ["Web前端"]
slug: "untitled"
draft: false
lastmod: "2011-06-01T13:50:50Z"
id: 1067156
url: "https://lanxing.iteye.com/blog/1067156"
---

以下方法在网上找到资料后修改过。

obj 对象就是个 字符串对象。

//添加千分号  
   
function AddQF(obj){  
    if(AllTrim(obj) != ""){  
        if(isDouble(AllTrim(obj))){  
            var str2= "";  
            if(AllTrim(obj).indexOf(".")!=-1){         
                var str = AllTrim(obj).split(".")\[0];  
                if(str.indexOf("-")!=-1){  
                    str = str.substring(1,str.length);  
                 }    
                 
                //添加千分号         
                var i = str.length/3;  
                var k = str.length%3;  
                if( k== 0){  
                    for(var j=0;j&lt;i;j++){             
                        str2+=str.substring(j\*3,3\*j+3)+",";         
                    }  
                }else{  
                    var l = int(i);  
                    str2+=str.substring(0,k)+",";  
                    for(var j=0;j&lt;l;j++){             
                        str2+=str.substring(j\*3+k,3\*j+3+k)+",";         
                    }  
                           
                }  
                //去除最后一个千分号         
                str2=str2.substring(0,str2.length-1);  
                str2+="."+AllTrim(obj).split(".")\[1];  
             }else{  
               var str = AllTrim(obj);        
                 if(str.indexOf("-")!=-1){  
                    str = str.substring(1,str.length);  
                 }  
                //添加千分号         
                var i = str.length/3;  
                var k = str.length%3;  
                if( k== 0){  
                    for(var j=0;j&lt;i;j++){             
                        str2+=str.substring(j\*3,3\*j+3)+",";         
                    }  
                }else{  
                    var l = int(i);  
                    str2+=str.substring(0,k)+",";  
                    for(var j=0;j&lt;l;j++){             
                        str2+=str.substring(j\*3+k,3\*j+3+k)+",";         
                    }  
                           
                }  
                //去除最后一个千分号         
                str2=str2.substring(0,str2.length-1);  
             }  
             if(AllTrim(obj).indexOf("-")!=-1){  
                obj = "-"+str2;  
             }else{  
                obj = str2;  
             }  
              
         }          
     }else{  
           }  
    return obj;  
     
}

//去处千分号  
function DelQF(obj){  
    obj = ReplaceAll(obj,",","") ;  
     setCaret(obj,obj.length);  
      
}

//显示全部文本  
function ShowText(obj){  
    obj.title=obj;  
}

//转换为整数  
function int(num){return num-num%1}

 

//替换字符串  
function ReplaceAll(str, sptr, sptr1)  
{  
    while (str.indexOf(sptr) &gt;= 0)  
    {  
       str = str.replace(sptr, sptr1);  
    }  
    return str;  
}

//是否为数字(含小数和负数)  
function isDouble(s)  
{  
var patrn=/^-?\[1-9]+\\d\*$|^(-?\[1-9]+\\d\*)(\\.\\d+)$|^(-?\[0])(\\.\\d+)$|^\[0]$/;  
if (!patrn.exec(s)) return false  
return true  
}

//是否为正整数  
function isInt(s)  
{  
var patrn=/^\\d\*$/;  
if (!patrn.exec(s)) return false  
return true  
}

//去所有空格    
String.prototype.trimAll = function(){    
    return this.replace(/(^\\s\*)|(\\s\*)|(\\s\*$)/g, "");    
};

function AllTrim(obj){    
    return obj.trimAll();  
}

//去两边空格    
String.prototype.trim = function(){    
    return this.replace(/(^\\s\*)|(\\s\*$)/g, "");    
};  
function PeripheralTrim(obj){    
    return obj.trim();  
}    
     

//光标移到text 中指定位置(选中文本)  
function setCaret(obj,pos)  
{      
    var r = obj.createTextRange();  
    //r.collapse(true);  
   // r.moveStart('character',pos);  
    r.select();  
}

//获取系统时间  
function showtime () {  
var now = new Date();  
var year = now.getYear();  
var month = now.getMonth() + 1;  
var date = now.getDate();  
var hours = now.getHours();  
var minutes = now.getMinutes();  
var seconds = now.getSeconds();  
var day = now.getDay();  
Day = new MakeArray(7);  
Day\[0]="星期天";  
Day\[1]="星期一";  
Day\[2]="星期二";  
Day\[3]="星期三";  
Day\[4]="星期四";  
Day\[5]="星期五";  
Day\[6]="星期六";  
var timeValue = "";  
timeValue += "&lt;span style='font-size:20px;'&gt;&lt;b&gt;";  
timeValue += ((hours &lt; 10) ? "0"+hours: hours);  
timeValue += ((minutes &lt; 10) ? ":0" : ":") + minutes;  
timeValue += ((seconds &lt; 10) ? ":0" : ":") + seconds;  
timeValue += (hours &lt; 12) ? "&lt;span style='font-size:12px;'&gt;上午&lt;/span&gt;" : "&lt;span style='font-size:12px;'&gt;下午&lt;/span&gt;";  
timeValue += "&lt;/span&gt; "+(Day\[day]) + " ";  
timeValue += year + "年";  
timeValue += ((month &lt; 10) ? "0" : "") + month + "月";  
timeValue += date + "日 ";

document.getElementById("time").innerHTML = timeValue+"&lt;/b&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;br/&gt;&lt;br/&gt;";  
timerID = setTimeout("showtime()",1000);  
timerRunning = true  
}