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
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' ) );
Avatar of leakim971
leakim971
Flag of Guadeloupe image

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 ?
Avatar of Big Monty
Big Monty
Flag of United States of America image

whats happening? error message? need more details
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Work fine for me : http://jsfiddle.net/bvF7N/
Avatar of cofactor
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
                        });

                  });
Avatar of cofactor
cofactor

ASKER

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

should I print alert(e1.type)  to check if all are span elements ?
Avatar of leakim971
leakim971
Flag of Guadeloupe image

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

Avatar of cofactor
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 ?
Avatar of leakim971
leakim971
Flag of Guadeloupe image

An issue?
Done : http://jsfiddle.net/bvF7N/7/
Avatar of cofactor
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 ?
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo