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, I need to manipulate a # that i will initially store in local storage then get it later. Initially, The # will be a pure # like '1122221112' - my question would be how would the JS look like to manipulate that # to look like '1,122,221,112'

Basically, i need to add a ',' after every third digit from right to left...Thanks

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

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

ok ill look into it. thanks

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

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

ok those are useful, I tried the 1st one (currency format)...im trying to change a long decimal value like ‘20.02788990’ into ’20.03’ for currency purposes. this is the original javascript code on the web:

--------------------------------------------------------------------------------
  1. function CurrencyFormatted(amount) {

  2. var i = parseFloat(amount);

  3. if(isNaN(i)) { i = 0.00; }

  4. var minus = '';

  5. if(i < 0) { minus = '-'; }

  6. i = Math.abs(i);

  7. i = parseInt((i + .005) * 100);

  8. i = i / 100;

  9. s = new String(i);

  10. if(s.indexOf('.') < 0) { s += '.00'; }

  11. if(s.indexOf('.') == (s.length - 2)) { s += '0'; }

  12. s = minus + s;

  13. return s;

  14. }
    15.

  15. /**

  16. * Usage: CurrencyFormatted(12345.678);

  17. * result: 12345.68

  18. **/
    -------------------------------------------------------------------------------------

    And this is how im implementing the code:

    ------------------------------------------------------------------------------------
    var a = localStorage.getItem('test1')

    function CurrencyFormatted('a') {
    var i = parseFloat('a');
    if(isNaN(i)) { i = 0.00; }
    var minus = '';
    if(i < 0) { minus = '-'; }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if(s.indexOf('.') < 0) { s += '.00'; }
    if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
    s = minus + s;
    }

    localStorage.setItem('test1', s)
    ----------------------------------------------------------------------------------------------

    But, when I run it firebug gives me this error:

    Image

    Im not sure what to do here? any ideas?

Illya Stepanov
Posts: 0
Joined: Mon Mar 18, 2013 8:48 am

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

Hi John, in your function after the parentheses ( ) you insert parameter code'a'<
remove single quotes ' - ' if you want to input in a function - "a" -- as a variable./code

Illya Stepanov
Posts: 0
Joined: Mon Mar 18, 2013 8:48 am

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

Hi John, in your function after the parentheses ( ) you insert parameter
code'a'/code
remove single quotes ' - ' if you want to input in a function - "a" -- as a variable.

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

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

ok so i removed the single quotes and my code looks like this now:

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

var a = localStorage.getItem('test1')

function CurrencyFormatted(a) {
var i = parseFloat(a);
if(isNaN(i)) { i = 0.00; }
var minus = '';
if(i < 0) { minus = '-'; }
i = Math.abs(i);
i = parseInt((i + .005) * 100);
i = i / 100;
s = new String(i);
if(s.indexOf('.') < 0) { s += '.00'; }
if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
s = minus + s;
}

localStorage.setItem('test1', s)

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

However, i now get this error:

Image

from what it looks like to me, s is the the result after the conversion... Is that not so here? And if so Im trying to store it in the local storage 'test1'...

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

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

Hello! In your JS code "s" is a local variable in function CurrencyFormatted. That's why you get this error. This code should work:
codefunction CurrencyFormatted(a) {
var i = parseFloat(a);
if(isNaN(i)) { i = 0&#46;00; }
var minus = '';
if(i < 0) { minus = '-'; }
i = Math&#46;abs(i);
i = parseInt((i + &#46;005) * 100);
i = i &#47; 100;
s = new String(i);
if(s&#46;indexOf('&#46;') < 0) { s += '&#46;00'; }
if(s&#46;indexOf('&#46;') == (s&#46;length - 2)) { s += '0'; }
s = minus + s;
return s;
}

localStorage&#46;setItem('test1', CurrencyFormatted(localStorage&#46;getItem('test1')));/code
Please note that debuggin custom JS code is outside the scope of our support.

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

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

yes that does work. I understand that, however when I run into Appery's language I usually ask when it involves implementation into Appery's platform. Thanks.

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

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

ok so i moved on to my original problem: adding commas. So, from that site that Igor sent me i used this JS:

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

function addCommas(nStr) {
nStr += '';
var x = nStr.split('.');
var x1 = x[0];
var x2 = x.length ; 1 ? '.' + x[1] : '';
var rgx = /(d+)(d{3})/;

Code: Select all

     while (rgx.test(x1)) { 
         x1 = x1.replace(rgx, '$1' + ',' + '$2'); 
     } 

     return x1 + x2; 
 } 
---------------------------------------------------------- 

and implemented it like this:

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

var nStr = localStorage.getItem('test2')

Code: Select all

 function addCommas(nStr) { 
     nStr += ''; 
     var x = nStr.split('.'); 
     var x1 = x[0]; 
     var x2 = x.length ; 1 ? '.' + x[1] : ''; 
     var rgx = /(d+)(d{3})/; 

     while (rgx.test(x1)) { 
         x1 = x1.replace(rgx, '$1' + ',' + '$2'); 
     } 

     return x1 + x2; 
 } 

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

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

But i get this error:

Image

How can i make this JS work? Thanks...

Return to “Issues”