<HTML>?
<BODY>?
<script? language=javascript>?
function? commafy(num)
{?
?? num? =? num+"";?
?? var? re=/(-?\d+)(\d{3})/?
?? while(re.test(num))
?? {?
???? num=num.replace(re,"$1,$2")?
?? }?
?? return? num;?
}
function? commafyback(num)
{?
?? var x = num.split(',');
?? return parseFloat(x.join(""));
}???
</script>?
<input? type=button? value="commafy"? onclick="alert(commafy(123456789.01))">?
<input? type=button? value="commafy"? onclick="alert(commafyback('123,456,789.01'))">?
</BODY>
</HTML>
join()默認是以逗號分隔。
當然程序還可以寫的多一點點,比如split 后,用 for in實現鏈接。
FeedBack:
# re: JAVASCRIPT 千位分隔符 以及去掉千位分隔符[未登錄]
2007-06-20 10:48 | 閑耘
對小數部分考慮似乎不周。下面的方法延用了你的方法名,但是查詞典不知道單詞加了-fy成了什么意思,請指教。
String.prototype.commafy = function(){
var _n = this.match(/(-?\d+)(\.\d*)?/);
var __n = _n[1].match(/(-?\d{0,3})((?:\d{3})*)/);
return __n[1] + __n[2].replace(/(\d{3})/g, ",$1") + // integer.
_n[2].replace(/(\d{3})/g, "$1,").replace(/(.*),$/, "$1"); // decimal.
};
Number.prototype.commafy = function(){
return this.toString().commafy();
};
String.prototype.decommafy = function(){
return this.replace(/,/g, "");
}; 回復 更多評論
String.prototype.commafy = function(){
var _n = this.match(/(-?\d+)(\.\d*)?/);
var __n = _n[1].match(/(-?\d{0,3})((?:\d{3})*)/);
return __n[1] + __n[2].replace(/(\d{3})/g, ",$1") + // integer.
_n[2].replace(/(\d{3})/g, "$1,").replace(/(.*),$/, "$1"); // decimal.
};
Number.prototype.commafy = function(){
return this.toString().commafy();
};
String.prototype.decommafy = function(){
return this.replace(/,/g, "");
}; 回復 更多評論
2008-06-11 12:40 | 鹿
樓上的整數部分有錯誤 , 比如 12345 只會返回 123
更正如下
String.prototype.commafy = function(){
if (this == "") return "";
var _n = this.match(/(-?\d+)(\.\d*)?/);
var length = _n[1].length;
var i = length % 3
var str1 = _n[1].substring(0,i)
var str2 = _n[1].substring(i,length)
var result = str1 + str2.replace(/(\d{3})/g, ",$1") + // integer.
_n[2].replace(/(\d{3})/g, "$1,").replace(/(.*),$/, "$1"); // decimal.
result = result.replace(/^,(.*)/, "$1")
return result;
};
回復 更多評論
更正如下
String.prototype.commafy = function(){
if (this == "") return "";
var _n = this.match(/(-?\d+)(\.\d*)?/);
var length = _n[1].length;
var i = length % 3
var str1 = _n[1].substring(0,i)
var str2 = _n[1].substring(i,length)
var result = str1 + str2.replace(/(\d{3})/g, ",$1") + // integer.
_n[2].replace(/(\d{3})/g, "$1,").replace(/(.*),$/, "$1"); // decimal.
result = result.replace(/^,(.*)/, "$1")
return result;
};
回復 更多評論
只有注冊用戶登錄后才能發表評論。 | ||
![]() |
||
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
|
||
常用鏈接
留言簿(21)
隨筆分類(265)
- android(1)
- c#(.net)(1)
- C++(33)
- DB(5)
- JAVA(38)
- JSP(17)
- linux(7)
- mac(10)
- nginx(3)
- PHP(20)
- python(19)
- UML(1)
- VML(1)
- WebService(2)
- web前端(71)
- XML(4)
- 感悟生活(5)
- 正則表達式(2)
- 計算機綜合(25)
隨筆檔案(242)
- 2012年5月 (1)
- 2012年4月 (2)
- 2011年12月 (9)
- 2011年11月 (20)
- 2011年3月 (8)
- 2010年10月 (15)
- 2010年6月 (2)
- 2010年4月 (1)
- 2010年3月 (7)
- 2010年1月 (4)
- 2009年12月 (1)
- 2009年10月 (1)
- 2009年9月 (1)
- 2009年8月 (8)
- 2009年6月 (11)
- 2009年5月 (3)
- 2009年4月 (7)
- 2008年11月 (5)
- 2008年10月 (6)
- 2008年8月 (1)
- 2008年7月 (6)
- 2008年6月 (5)
- 2008年5月 (1)
- 2008年4月 (18)
- 2008年3月 (1)
- 2008年2月 (1)
- 2007年12月 (9)
- 2007年9月 (5)
- 2007年8月 (6)
- 2007年7月 (4)
- 2007年6月 (6)
- 2007年5月 (8)
- 2007年4月 (9)
- 2007年3月 (6)
- 2007年2月 (44)
相冊
JAVA網站
關注的Blog
搜索
積分與排名
- 積分 - 914066
- 排名 - 40
最新評論

- 1.?re: IP地址獲取當前地理位置接口
- fdf
- --fdfs
- 2.?re: 淺談comet技術
- slowhttp攻擊會直接把server搞垮 因為通常這種應用場景的http的timeout值會比較大
- --harlan
- 3.?re: php動態圖片驗證碼
- 額
- --人
- 4.?re: 關于傳值和傳引用的插曲
- 大家不要看這篇文章 本來很簡單的事情反而被博主繞復雜了,
- --bandit