Results 1 to 9 of 9

Thread: Set Commas (,) to the Value

  1. #1
    Join Date
    Nov 2006
    Location
    Jakarta
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Set Commas (,) to the Value

    Hi guys,
    Need Help with javascript ...

    Let's say i have a textbox, i fill it with a numeric value like 50000000. could anyone help me how to change that value with 50,000,000 when i lost focus with the textbox (onBlur).And I also need to add 2 digits behind the comma (decimal) like 7,500.21 ... Could be a function right ? help me please ...

    Thank You

  2. #2
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Here's code that will format numbers to have commas in them, and it will guarantee 2 decimal points at the end, either by adding them or truncating to that amount. It will even work with numbers that already have commas in them.

    **fixed to accomodate negative numbers**
    Code:
    var field = document.getElementById("idHere");
    
    field.onblur = function(e) {
    	var w = this.value.replace(/[, ]/g, "");
    	if(isNaN(w*1)) {
    		alert("Please enter a number.");
    		return;
    		}
    	var d = ".00", v = "", s = false, p;
    	if((p=w.indexOf("."))!=-1) {
    		d = w.substring(p);
    		w = w.substring(0, p);
    		}
    	while(d.length<3) d += "0";
    	d = d.substring(0, 3);
    	if((p=w.charAt(0))=="+") w = w.substring(1);
    	else if(p=="-") {
    		w = w.substring(1);
    		s = true;
    		}
    	var n = w.length;
    	for(var i=0, j=n-1; i<n; ++i, --j) {
    		v += w.charAt(i);
    		if(j&#37;3==0) v += ",";
    		}
    	this.value = (s? "-": "") + v.substring(0, v.length-1) + d;
    	}
    Last edited by Trinithis; 08-31-2007 at 06:00 AM.
    Trinithis

  3. #3
    Join Date
    Aug 2007
    Location
    Somewhere in the vicinity of Betelgeuse
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I'm sure there's a better way of doing this, but it's early and I haven't finished my coffee yet:
    Code:
    	function formatNumber(number, digits) {
    		var d = digits > 0 ? number.toFixed(digits).substr(number.toFixed(digits).indexOf(".")) : "";
    		var n = parseInt(number).toString();
    		var r = "";
    		for(var i = n.length; i >= -2; i -= 3) {
    			var l = 3;
    			if(i < 0) {
    				l += i;
    				i = 0;
    			}
    			if(r != "") {
    				r = "," + r;
    			}
    			r = n.substr(i, l) + r;
    		}
    		return r + d;
    	}

  4. #4
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    this.value = this.value.split('.')[0].split('').reverse().join('').match(/\d{3}|\d+$/g).join(',').split('').reverse().join('')+'.'+parseFloat(this.value).toFixed(2).toString().split('.')[1];
    Last edited by Twey; 08-31-2007 at 08:16 AM.
    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!

  6. #6
    Join Date
    Nov 2006
    Location
    Jakarta
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Trinithis View Post
    Here's code that will format numbers to have commas in them, and it will guarantee 2 decimal points at the end, either by adding them or truncating to that amount. It will even work with numbers that already have commas in them.

    **fixed to accomodate negative numbers**
    Code:
    var field = document.getElementById("idHere");
    
    field.onblur = function(e) {
    	var w = this.value.replace(/[, ]/g, "");
    	if(isNaN(w*1)) {
    		alert("Please enter a number.");
    		return;
    		}
    	var d = ".00", v = "", s = false, p;
    	if((p=w.indexOf("."))!=-1) {
    		d = w.substring(p);
    		w = w.substring(0, p);
    		}
    	while(d.length<3) d += "0";
    	d = d.substring(0, 3);
    	if((p=w.charAt(0))=="+") w = w.substring(1);
    	else if(p=="-") {
    		w = w.substring(1);
    		s = true;
    		}
    	var n = w.length;
    	for(var i=0, j=n-1; i<n; ++i, --j) {
    		v += w.charAt(i);
    		if(j&#37;3==0) v += ",";
    		}
    	this.value = (s? "-": "") + v.substring(0, v.length-1) + d;
    	}
    I'm a little bit confused here ... how to call this function from the the textbox i have ? I already try but failed

  7. #7
    Join Date
    Jun 2006
    Posts
    182
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default

    Quote Originally Posted by Twey View Post
    Code:
    this.value = this.value.split('.')[0].split('').reverse().join('').match(/\d{3}|\d+$/g).join(',').split('').reverse().join('')+'.'+parseFloat(this.value).toFixed(2).toString().split('.')[1];
    There's a small bug in that line if the number is '-1' or '-10' an error occures. You look for one-or-more-digits at the end, which is not found because of the minus sign, so match() returns null. Also minus signs are ignored. (Though the OP may not even need them.)

    Anyway, here's another variant of it, which can handle minus signs:
    Code:
    this.value = this.value.split('.')[0].split('').reverse().join('').match(/(?:\d{3}|\d+)(?:-$)?/g).join(',').split('').reverse().join('')+'.'+parseFloat(this.value).toFixed(2).toString().split('.')[1];
    And to make it work you just insert this snippet into the onchange (or onblur, if you wish) attribute of your text input. If you have multiple text inputs to be checked, you should make it a function though.

  8. #8
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    You would have to give your input field an ID, and change the getElementById()'s argument accordingly. In any case, I think DimX's code is better.
    Trinithis

  9. #9
    Join Date
    Nov 2006
    Location
    Jakarta
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default It's Work Fine

    Quote Originally Posted by DimX View Post
    There's a small bug in that line if the number is '-1' or '-10' an error occures. You look for one-or-more-digits at the end, which is not found because of the minus sign, so match() returns null. Also minus signs are ignored. (Though the OP may not even need them.)

    Anyway, here's another variant of it, which can handle minus signs:
    Code:
    this.value = this.value.split('.')[0].split('').reverse().join('').match(/(?:\d{3}|\d+)(?:-$)?/g).join(',').split('').reverse().join('')+'.'+parseFloat(this.value).toFixed(2).toString().split('.')[1];
    And to make it work you just insert this snippet into the onchange (or onblur, if you wish) attribute of your text input. If you have multiple text inputs to be checked, you should make it a function though.
    Thank's DimX, tour code is work ...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •