-1

I need to simply add commas to numbers in my code. I have tried a lot of hints here but something seems to be wrong.

I have tried with regex and the toLocalString() method and both haven't worked, can someone help me find the correct solution.

<script type="text/javascript">
   $(document).ready(function () {
      // $("#price").toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')
      $('#price').html.toLocaleString('en');
   });
</script>

Here's my element:

<strong id="price"><%# Eval("OfferPrice") %></strong>

Everything seems ok, can anyone help please. Thanks a lot!

Studocwho
  • 2,404
  • 3
  • 23
  • 29
noetico
  • 25
  • 6
  • Possible duplicate of [How to print a number with commas as thousands separators in JavaScript](https://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript) – Bl00D4NGEL May 30 '19 at 00:44
  • To clarify your question, are you trying to add commas after any instance of a number? If so I don't see any number in the string- 'Everything seems ok, can anyone help please. Thanks a lot!' – MAB May 30 '19 at 00:43
  • Its an ASP.Net project; the number, is pulled from a databind in codebehind using the <%# Eval("OfferPrice") %> which outputs the rate – noetico May 30 '19 at 07:59
  • This is the output on the page: Budget: ```1000000 Job Specification Document(Opens in New Window): View/Download Task Description: ``` – noetico May 30 '19 at 08:03

4 Answers4

0

If you trying to add commas after any instance of a number, you need to have a number in the output string 'Everything seems ok, can anyone help please. Thanks a lot!'

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
MAB
  • 43
  • 2
  • 7
  • Its an ASP.Net project; the number, is pulled from a databind in codebehind using the <%# Eval("OfferPrice") %> which outputs the rate – noetico May 30 '19 at 08:00
0

From MDN documentation :

parseFloat($('#price')).toLocaleString('en-US');

You need to specify that the value of #price is of type number (float), then you can apply toLocalString() function with your desired formatting country.

Xavier Brassoud
  • 697
  • 6
  • 14
  • I have done this, still it doesn't work. `parseFloat($('#price')).toLocaleString('en-US')` and also: `function numberWithCommas(x) { return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); } numberWithCommas(('#price'))` – noetico May 30 '19 at 08:58
0

So, I finally found a 'mix' from the answers; that works. Here's the code. Thanks guys... and I will still need more help

   $(document).ready(function () {

        var x = parseFloat($('#price').html()).toLocaleString('en-US');
         $('#price').html(x)


   });

Works awesome but will render only the first instance; how do I make it run through every instance of id="price" in the DOM? Thanks a lot friends.

noetico
  • 25
  • 6
0

Replace in your DOM all id attributes by class attributes, then you can render all values by your desired formatting.

Your DOM elements will be edited as follow :

<strong class="price"><%# Eval("OfferPrice") %></strong>

From jQuery Documentation :

$(document).ready(function () {
        var x = parseFloat($('.price').html()).toLocaleString('en-US');
        $('.price').html(x)
 });
Xavier Brassoud
  • 697
  • 6
  • 14
  • This just renders the value of the first element across all elements with the same class, so instead of 1,000... 3,000, 12,000; I have 1,000 across all. This is what I have also tried `$('#price').each(function () { var x = parseFloat($('#price').html()).toLocaleString('en-US'); $('#price').html(x); });` but this still affects only the first element with selected id – noetico May 30 '19 at 11:06