John Herdean
Posts: 0
Joined: Tue May 14, 2013 3:56 am

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

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

Alena Prykhodko
Posts: 0
Joined: Tue Apr 09, 2013 7:36 am

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

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

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

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

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

John Herdean
Posts: 0
Joined: Tue May 14, 2013 3:56 am

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

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

John Herdean
Posts: 0
Joined: Tue May 14, 2013 3:56 am

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

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

John Herdean
Posts: 0
Joined: Tue May 14, 2013 3:56 am

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

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.

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

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

Hi John,

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

John Herdean
Posts: 0
Joined: Tue May 14, 2013 3:56 am

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

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

hugues.aubuchon
Posts: 0
Joined: Wed Jul 24, 2013 10:40 pm

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

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

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

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

Hi!
Thank you for sharing this.

Return to “Issues”