当前位置: 代码迷 >> Web前端 >> 平添千分号
  详细解决方案

平添千分号

热度:219   发布时间:2012-10-19 16:53:36.0
添加千分号

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

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<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<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<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<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) >= 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 += "<span style='font-size:20px;'><b>";
timeValue += ((hours < 10) ? "0"+hours: hours);
timeValue += ((minutes < 10) ? ":0" : ":") + minutes;
timeValue += ((seconds < 10) ? ":0" : ":") + seconds;
timeValue += (hours < 12) ? "<span style='font-size:12px;'>上午</span>" : "<span style='font-size:12px;'>下午</span>";
timeValue += "</span> "+(Day[day]) + " ";
timeValue += year + "年";
timeValue += ((month < 10) ? "0" : "") + month + "月";
timeValue += date + "日 ";

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

?

  相关解决方案