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)
JSPJavaScript

Avatar of undefined
Last Comment
leakim971

8/22/2022 - Mon
Big Monty

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

alert( numberWithCommas( 1234567.458' ) );
leakim971

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
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 ?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Big Monty

whats happening? error message? need more details
leakim971

Work fine for me : http://jsfiddle.net/bvF7N/
cofactor

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

                  });
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
leakim971

cofactor

ASKER
How do I debug  my code ...whats wrong ?

should I print alert(e1.type)  to check if all are span elements ?
leakim971

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

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
cofactor

ASKER
@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 ?
leakim971

An issue?
Done : http://jsfiddle.net/bvF7N/7/
cofactor

ASKER
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 ?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
leakim971

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question