-5

I'm not too familiar with Javascript so there's probably a really simple solution to this. All I want to do is add comma thousands separators to the output of this function:

function() {
return '$' + this.value;
}

So, for instance $100000 will display as $100,000. My problem is that I don't know how to deal with the variable this.value because of the period.

I tried:

function numberWithCommas(this.value) {
    return '$' + this.value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

My guess is that this.value.toString() is not proper syntax.

Thanks in advance.

rfsch
  • 1
  • 1
  • 5
    What have you tried to far? SO isn't a free code writing service. –  Jun 14 '17 at 14:54
  • 2
    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) – Liam Jun 14 '17 at 14:56

2 Answers2

0

Hope it will help you:

var n = 1123456789.123
n.toLocaleString()
"1,123,456,789.123"

See more: toLocaleString

  1. You can use: Numeral.js - it is very powerful

And: in the future, you really research and try again and again.

Thanks

Alex
  • 3,646
  • 1
  • 28
  • 25
0

This worked:

function numberWithCommas() {
    return '$' + this.value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
rfsch
  • 1
  • 1