PDA

View Full Version : Set Commas (,) to the Value



baltazhor200401
08-31-2007, 02:05 AM
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 :)

Trinithis
08-31-2007, 05:40 AM
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**


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;
}

Mr Moo
08-31-2007, 05:57 AM
I'm sure there's a better way of doing this, but it's early and I haven't finished my coffee yet:


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;
}

codeexploiter
08-31-2007, 06:48 AM
Check the following items

Adding Comma to a number (http://www.mredkj.com/javascript/nfbasic.html)
Number Formatting - Additionals (http://www.mredkj.com/javascript/nfdocs.html)

Twey
08-31-2007, 08:08 AM
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];

baltazhor200401
08-31-2007, 10:17 AM
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**


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

DimX
08-31-2007, 11:46 AM
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 :p 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:


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.

Trinithis
08-31-2007, 03:51 PM
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.

baltazhor200401
09-05-2007, 12:37 AM
There's a small bug in that line :p 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:


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 ...