Link to home
Start Free TrialLog in
Avatar of cofactor
cofactor

asked on

comma seperator value in javascript

I need a java script function which will put comma separation (currency format) in amounts

Input :  4000000.00
output: 4,000,000.00


Input: 1234567.458
Output:1,234,567.46 (rounded)
Avatar of Big Monty
Big Monty
Flag of United States of America image

function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",").toFixed(2);
}

alert( numberWithCommas( 1234567.458' ) );
Test page : http://jsfiddle.net/Va5Fy/
function f(n) {
  return (Math.round(n*100)/100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

Open in new window


Need more powerful check here the Decimal adjustment:
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Math/floor
Avatar of cofactor
cofactor

ASKER

This is not working ....

jQuery(document).ready(
                  function() {                        

                        $(".cm_856231kj").each(function(index, el) {
                              var amount=$(el).html();
                              $(el).html(numberWithCommas(amount));
                        });

});

whats wrong ?
whats happening? error message? need more details
please note  $(el)  is a span element.  I  am trying to replace  span  content with comma formatted  amount .....however the code below does not work.

jQuery(document).ready(
                  function() {
                        var cm_856231kj_arr=$(".cm_856231kj");                  
                        $(".cm_856231kj").each(function(index, el) {
                              var amount=$(el).html();
                              alert('hello='+amount);  <-------I  get alert here .
                              $(el).html(numberWithCommas(amount));  .
                                       <-------- value not set ..loop beraks
                        });

                  });
How do I debug  my code ...whats wrong ?

should I print alert(e1.type)  to check if all are span elements ?
How do I debug  my code ...whats wrong ?
Your code have nothing wrong but the function from The_Big_Daddy you're using...
So using mine maybe a good idea...
If you LOVE the name of the function, just replace :
function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",").toFixed(2);
}

Open in new window

by :
function  numberWithCommas(n) {
  return (Math.round(n*100)/100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

Open in new window

@leakim971

There is a issue in your JavaScript. function

when I put  a  whole number   say .... n=124

It gives  output 124.00

I want output 124 here.

Can  you please modify your code ?
I applied this ...

if(n % 1 != 0){ //n is decimal
                  return (Math.round(n*100)/100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
            }else{ // n is whole number
                  return n.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
            }



How is it ?
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial