js格式化金額可選是否帶千分位以及保留精度
更新時(shí)間:2014年01月28日 15:21:23 作者:
網(wǎng)上搜到的js格式化金額可選是否帶千分位以及保留精度,還不錯(cuò),大家可以學(xué)習(xí)下
js格式化金額,可選是否帶千分位,可選保留精度,也是網(wǎng)上搜到的,但是使用沒問題
/*
將數(shù)值四舍五入后格式化.
@param num 數(shù)值(Number或者String)
@param cent 要保留的小數(shù)位(Number)
@param isThousand 是否需要千分位 0:不需要,1:需要(數(shù)值類型);
@return 格式的字符串,如'1,234,567.45'
@type String
*/
function formatNumber(num,cent,isThousand){
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))//檢查傳入數(shù)值為數(shù)值類型.
num = "0";
if(isNaN(cent))//確保傳入小數(shù)位為數(shù)值型數(shù)值.
cent = 0;
cent = parseInt(cent);
cent = Math.abs(cent);//求出小數(shù)位數(shù),確保為正整數(shù).
if(isNaN(isThousand))//確保傳入是否需要千分位為數(shù)值類型.
isThousand = 0;
isThousand = parseInt(isThousand);
if(isThousand < 0)
isThousand = 0;
if(isThousand >=1) //確保傳入的數(shù)值只為0或1
isThousand = 1;
sign = (num == (num = Math.abs(num)));//獲取符號(hào)(正/負(fù)數(shù))
//Math.floor:返回小于等于其數(shù)值參數(shù)的最大整數(shù)
num = Math.floor(num*Math.pow(10,cent)+0.50000000001);//把指定的小數(shù)位先轉(zhuǎn)換成整數(shù).多余的小數(shù)位四舍五入.
cents = num%Math.pow(10,cent); //求出小數(shù)位數(shù)值.
num = Math.floor(num/Math.pow(10,cent)).toString();//求出整數(shù)位數(shù)值.
cents = cents.toString();//把小數(shù)位轉(zhuǎn)換成字符串,以便求小數(shù)位長(zhǎng)度.
while(cents.length<cent){//補(bǔ)足小數(shù)位到指定的位數(shù).
cents = "0" + cents;
}
if(isThousand == 0) //不需要千分位符.
return (((sign)?'':'-') + num + '.' + cents);
//對(duì)整數(shù)部分進(jìn)行千分位格式化.
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + num + '.' + cents);
}
復(fù)制代碼 代碼如下:
/*
將數(shù)值四舍五入后格式化.
@param num 數(shù)值(Number或者String)
@param cent 要保留的小數(shù)位(Number)
@param isThousand 是否需要千分位 0:不需要,1:需要(數(shù)值類型);
@return 格式的字符串,如'1,234,567.45'
@type String
*/
function formatNumber(num,cent,isThousand){
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))//檢查傳入數(shù)值為數(shù)值類型.
num = "0";
if(isNaN(cent))//確保傳入小數(shù)位為數(shù)值型數(shù)值.
cent = 0;
cent = parseInt(cent);
cent = Math.abs(cent);//求出小數(shù)位數(shù),確保為正整數(shù).
if(isNaN(isThousand))//確保傳入是否需要千分位為數(shù)值類型.
isThousand = 0;
isThousand = parseInt(isThousand);
if(isThousand < 0)
isThousand = 0;
if(isThousand >=1) //確保傳入的數(shù)值只為0或1
isThousand = 1;
sign = (num == (num = Math.abs(num)));//獲取符號(hào)(正/負(fù)數(shù))
//Math.floor:返回小于等于其數(shù)值參數(shù)的最大整數(shù)
num = Math.floor(num*Math.pow(10,cent)+0.50000000001);//把指定的小數(shù)位先轉(zhuǎn)換成整數(shù).多余的小數(shù)位四舍五入.
cents = num%Math.pow(10,cent); //求出小數(shù)位數(shù)值.
num = Math.floor(num/Math.pow(10,cent)).toString();//求出整數(shù)位數(shù)值.
cents = cents.toString();//把小數(shù)位轉(zhuǎn)換成字符串,以便求小數(shù)位長(zhǎng)度.
while(cents.length<cent){//補(bǔ)足小數(shù)位到指定的位數(shù).
cents = "0" + cents;
}
if(isThousand == 0) //不需要千分位符.
return (((sign)?'':'-') + num + '.' + cents);
//對(duì)整數(shù)部分進(jìn)行千分位格式化.
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + num + '.' + cents);
}
相關(guān)文章
javascript實(shí)現(xiàn)點(diǎn)擊圖片切換功能
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)點(diǎn)擊圖片切換功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07BootStrap tab選項(xiàng)卡使用小結(jié)
這篇文章主要為大家詳細(xì)介紹了BootStrap tab選項(xiàng)卡使用小結(jié),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10javascript break指定標(biāo)簽打破多層循環(huán)示例
break的語(yǔ)法有兩種break; 和 break label;下面為大家介紹下直接break掉整個(gè)循環(huán)嵌套示例2014-01-01JS中的hasOwnProperty()、propertyIsEnumerable()和isPrototypeOf()
這篇文章主要介紹了JS中的hasOwnProperty()、propertyIsEnumerable()和isPrototypeOf()的相關(guān)資料,需要的朋友可以參考下2016-08-08javascript實(shí)現(xiàn)左右控制無(wú)縫滾動(dòng)
這篇文章主要介紹了javascript實(shí)現(xiàn)左右控制無(wú)縫滾動(dòng)的方法及示例代碼,需要的朋友可以參考下2014-12-12