Mike Cole
Mike Cole

Reputation: 14753

Numbers with commas in Javascript

I have a javascript function that accepts a number and performs a mathematical operation on the number. However, the number I'm passing in could have a comma in it, and from my limited experience with Javascript I am having problems working with that value. It doesn't seem to treat that as a numeric type.

What's the easiest way to take a parameter with a value of 1,000 and convert it to a numeric 1000?

Upvotes: 27

Views: 52317

Answers (4)

vsync
vsync

Reputation: 130730

Updated Answer:

Comma-delimited numbers are language-specific and not all uses this system.

There are many wrong/incomplete answers on Stackoverflow, which are tailored for a specific local, or suggesting a 3rd-party script from NPM, which I believe to be an overkill.

Here's a list of not-good-at-all answers:

  1. How do I convert String to Number according to locale (opposite of .toLocaleString)?
  2. Is there any JavaScript standard API to parse to number according to locale?

There is one particular good answer which addresses multiple locales:

https://stackoverflow.com/a/45309230/104380

Previous Answer:

Converts comma delimited number string into a type number (aka type casting)

+"1,234".split(',').join('') // outputs 1234

Breakdown:

+             - math operation which casts the type of the outcome into type Number
"1,234"       - Our string, which represents a comma delimited number
.split(',')   - split the string into an Array: ["1", "234"], between every "," character
.join('')     - joins the Array back, without any delimiter: "1234"

And a simple function would be:

function stringToNumber(s){
  return +s.split(',').join('');
}

Upvotes: 3

TStamper
TStamper

Reputation: 30384

You can set up your textbox to have an onblur() function so when the user attempts to leave the textbox, you then remove the commas from the value by using the javascript replace function

example:

  function checkNumeric(objName)
  {
    var lstLetters = objName;

    var lstReplace = lstLetters.replace(/\,/g,'');
  }  

With input tag here:

<input type="text" onblur="checkNumeric(this);" name="nocomma" size="10" maxlength="10"/>

Upvotes: 34

johnvey
johnvey

Reputation: 5151

A quick and dirty way is to use the String.replace() method:

var rawstring = '1,200,000';
var cleanstring = rawstring.replace(/[^\d\.\-\ ]/g, '');

This will set cleanstring to: 1200000. Assuming you are using US formatting, then the following conversions will occur:

1234 --> 1234
1,234 --> 1234
-1234 --> -1234
-1,234 --> -1234
1234.5 --> 1234.5
1,234.5 --> 1234.5
-1,234.5 --> -1234.5
1xxx234 --> 1234

If you are in other locales that invert the '.' and ',', then you'll have to make that change in the regex.

Upvotes: 23

James Black
James Black

Reputation: 41838

Just replace all the commas with a blank.

You can follow this: http://blog.techsaints.com/2007/06/25/javascript-how-to-remove-all-commas-from-a-number/

Upvotes: 2

Related Questions