Page 2 of 2

how to add a ',' after every third digit from right to left?

Posted: Tue Jul 23, 2013 12:27 am
by John Herdean

this code works fine if your dealing with a # that has 2 decimal places (i.e. 1223.00 will return 1,223.00 - but 1223 will crash and give error):

----------------------------------------------------------------------

var amount = localStorage.getItem('test2')

function CommaFormatted(amount) {
var delimiter = ","; // replace comma if desired
amount = new String(amount);
var a = amount.split('.',2)
var d = a[1];
var i = parseInt(a[0]);
if(isNaN(i)) { return ''; }
var minus = '';
if(i < 0) { minus = '-'; }
i = Math.abs(i);
var n = new String(i);
var a = [];
while(n.length 3)
{
var nn = n.substr(n.length-3);
a.unshift(nn);
n = n.substr(0,n.length-3);
}
if(n.length 0) { a.unshift(n); }
n = a.join(delimiter);
if(d.length < 1) { amount = n; }
else { amount = n + '.' + d; }
amount = minus + amount;
return amount;
}

localStorage.setItem('test2', CommaFormatted(localStorage.getItem('test2')));

-----------------------------------------------------------------------------

I need to be able to just add commas to whatever the # is, regardless of decimals


how to add a ',' after every third digit from right to left?

Posted: Tue Jul 23, 2013 12:30 am
by Alena Prykhodko

John, try to replace with the following
codevar x2 = (x&#46;length >1) ? '&#46;' + x[1] : '';/code


how to add a ',' after every third digit from right to left?

Posted: Tue Jul 23, 2013 7:53 am
by Kateryna Grynko

Hi John,

Here is the correct function:precodefunction CommaFormatted( amount ) {
amount = amount + '';
var delimiter = ",", &#47;&#47; replace comma if desired
a = amount&#46;split( '&#46;', 2 ),
d = '' || a[1],
i = parseInt( a[0], 10 );
if ( isNaN(i) ) {
return '';
}
var minus = '';
if ( i < 0 ) {
minus = '-';
}
i = Math&#46;abs( i );
var n = i + '';
a = [];
while ( n&#46;length > 3 ) {
var nn = n&#46;substr( n&#46;length - 3 );
a&#46;unshift(nn);
n = n&#46;substr( 0, n&#46;length - 3 );
}
if ( n&#46;length > 0 ) {
a&#46;unshift( n );
}
n = a&#46;join( delimiter );
if ( d&#46;length < 1 ) {
amount = n;
} else {
amount = n + '&#46;' + d;
}
amount = minus + amount;
return amount;
}/code/pre


how to add a ',' after every third digit from right to left?

Posted: Tue Jul 23, 2013 1:40 pm
by John Herdean

Hi Alena, changing that line will eliminate the error, but the function doesnt do anything. It wont add the commas.


how to add a ',' after every third digit from right to left?

Posted: Tue Jul 23, 2013 1:43 pm
by John Herdean

Hi Katya, yes that code works for numbers that have 2 decimal places (i.e. 1223.00 - the result will be 1,223.00). But if you gave it a # like this: 1223 - it will give this error:

Image


how to add a ',' after every third digit from right to left?

Posted: Tue Jul 23, 2013 1:44 pm
by John Herdean

the only way that code will work if i have another code to strip the decimal places because I have some cases i dont need it. Thanks.


how to add a ',' after every third digit from right to left?

Posted: Tue Jul 23, 2013 2:13 pm
by Kateryna Grynko

Hi John,

Sorry, our mistake.
Replace:coded = '' a[1],/code
with the following:coded = a[1] '',/code


how to add a ',' after every third digit from right to left?

Posted: Tue Jul 23, 2013 4:09 pm
by John Herdean

Great thank you much. That code now works with or without decimals and returns the number with just added comas.


how to add a ',' after every third digit from right to left?

Posted: Fri Jul 26, 2013 1:40 pm
by hugues.aubuchon

I have successfully used this library for all my number formatting needs. It seems pretty powerful and easy to use.

http://josscrowcroft.github.io/accoun...


how to add a ',' after every third digit from right to left?

Posted: Fri Jul 26, 2013 1:56 pm
by Kateryna Grynko

Hi!
Thank you for sharing this.