-1

I dont know whether this question is a duplicate or not because i can't find solution for my case.

I have a number ,say 12345 and i want to apply thousand seperator as comma(,) and the code for that is

12345.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");

and i am getting as 12,345 as a string. I want it as a number.

If i use "Number()" or "parseFloat()", it's coming as NaN. I have no idea how to convert the string with thousand seperator to Number with thousand seperator

Is there any solution for my case?

Arun AK
  • 4,353
  • 2
  • 23
  • 46
  • 4
    number cannot contain `,` – Rajaprabhu Aravindasamy Aug 10 '15 at 12:54
  • 1
    A *"Number with thousand seperator"* doesn't exist. A number is just a number (more precisely its domain is the one defined by IEEE754 double precision). What's your exact goal ? – Denys Séguret Aug 10 '15 at 12:55
  • 2
    delimiters makes the number as a string. This is how it is. – joyBlanks Aug 10 '15 at 12:55
  • Yes. But is there any solution for my case? – Arun AK Aug 10 '15 at 12:57
  • 3
    @Thinker: We don't even really understand what your problem is. Why do you want to convert a number to a thousand-delimited string and back? Why not just use the number `12345` that you already have? – Bergi Aug 10 '15 at 12:58
  • Hi, check this one. http://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript – M3ghana Aug 10 '15 at 13:00
  • 1
    Hi @Thinker, if you are looking to use the numeric value 12345 in a formula/calculation, you wouldn't need a comma in it, correct? And if you were looking to have the comma in there for display purposes, wouldn't a string with a comma in it meet your needs? I think there's some confusion on this point. – kyle_13 Aug 10 '15 at 13:05
  • Why not store the number in a variable/hidden input and the text in another variable/hidden input? this way you can easily access both – Alex Tartan Aug 10 '15 at 13:13
  • possible duplicate of [CSS3: Is it possible to have thousands separator in numbers?](http://stackoverflow.com/questions/9173227/css3-is-it-possible-to-have-thousands-separator-in-numbers) – A. Rama Aug 10 '15 at 14:33

1 Answers1

0

The common solution is to write a function that adds the comma:

//copied from http://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript
function numberWithCommas(x) {
    var parts = x.toString().split(".");
    parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    return parts.join(".");
}

A better solution might be to override the default toString() method so you don't have to convert it back and forth every time you do calculations:

Number.prototype.toString = function numToString() {
    var parts = x.toString().split(".");
    parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    return parts.join(".");
}

You would assign and manipulate the number as you would any normal number but it adds the comma when you go to print it.

TheIronCheek
  • 1,077
  • 2
  • 20
  • 50
  • Overriding `toString` does not sound like a very good idea to me. It's likely to break things that expect `toString` to return a valid number. I'd rather add a new function to the prototype. – Matti Virkkunen Aug 10 '15 at 13:29
  • ...not to mention that your overridden function would not work (`x` is not defined) – Matti Virkkunen Aug 10 '15 at 13:53