Hi,
I'm wondering how you would add commas to a number, say, in an <input> tag.
Ex.
1
12
123
1,234
12,345
123,456
1,234,567
I really want to know how to do this! I've seen similar scripts, but it is very confusing. Thank-you.
Hi,
I'm wondering how you would add commas to a number, say, in an <input> tag.
Ex.
1
12
123
1,234
12,345
123,456
1,234,567
I really want to know how to do this! I've seen similar scripts, but it is very confusing. Thank-you.
- Mike
Code:Number.prototype.withThousandsSeparator = function(sep) { var s = this.toString(), y = (s.indexOf(".") > -1 ? s.substring(s.indexOf(".")) : ""); if(y) s = s.substring(0, s.indexOf(".")); for(var i = s.length - 3;i > 0;i -= 3) { var front = s.substring(0, i), back = s.substring(i); s = front + sep + back; } return s + y; }
Last edited by Twey; 07-22-2006 at 11:55 PM.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
How do I access this function?
- Mike
Code:var num = 1234567; window.alert(num.withThousandsSeparator(','));
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Wow you're good
- Mike
Bookmarks