javascript parseInt to remove spaces from a string

passion picture passion · Jan 18, 2017 · Viewed 10.6k times · Source

I have an example of data that has spaces between the numbers, however I want to return the whole number without the spaces:

mynumber = parseInt("120 000", 10);
console.log(mynumber); // 120

i want it to return 120000. Could somebody help me with this? thanks

update

the problem is I have declared my variable like this in the beginning of the code:

var mynumber = Number.MIN_SAFE_INTEGER;

apparently this is causing a problem with your solutions provided.

Answer

Mike Cluck picture Mike Cluck · Jan 18, 2017

You can remove all of the spaces from a string with replace before processing it.

var input = '12 000';
// Replace all spaces with an empty string
var processed = input.replace(/ /g, '');
var output = parseInt(processed, 10);
console.log(output);