2008年8月1日 星期五

javascript reverse string , and add, for number

Q: add the ',' symbol in the number, like 1,234,567
reference:
http://www.javascripter.net/faq/converti.htm
http://www.ruby-forum.com/topic/79885
maxsize(x), maxlength(o)

http://www.bytemycode.com/snippets/snippet/400/
http://www.irt.org/script/1325.htm

>>> '123456'.split(/.../)
["", "", ""]
>>> '123456'.split(/(...)/)
["", "123", "", "456", ""]

ie split有問題, 所以只好自己做, 在下面


function fn_price(val) {
var val = val+'';

var a = val.split('').reverse();

var str = [];
for(var i = 0 ; i< a.length; i++){
if(i % 3 == 0 && i != 0) { str.push(',');}
str.push( a[i]);
}
return str.reverse().join('');
}

沒有留言: