Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript parseInt to remove spaces from a string

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.

like image 508
passion Avatar asked Jan 18 '17 21:01

passion


People also ask

Does parseInt remove spaces?

The parseInt() method by default ignores all the spaces around the string and targets the strings exactly from where the exact string is being started.

How do you Removes spaces from a string JS?

JavaScript String trim() The trim() method removes whitespace from both sides of a string. The trim() method does not change the original string.

How do I remove extra spaces from a string?

Use JavaScript's string. replace() method with a regular expression to remove extra spaces. The dedicated RegEx to match any whitespace character is \s .

How do you get rid of the middle space in a string?

We can use replace() to remove all the whitespaces from the string. This function will remove whitespaces between words too.


3 Answers

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);
like image 144
Mike Cluck Avatar answered Sep 30 '22 07:09

Mike Cluck


  1. Remove all whitespaces inside string by a replace function.
  2. using the + operator convert the string to number.

var mynumber = Number.MIN_SAFE_INTEGER;
mynumber = "120 000";
mynumber = mynumber.replace(" ", ""); 
console.log(+mynumber );
like image 30
Abhinav Galodha Avatar answered Sep 30 '22 08:09

Abhinav Galodha


You can replace all white space with replace function

var mynumber = "120 000";
console.log(mynumber.replace(/ /g,''));

OutPut is 120000

like image 20
Avinash Kumar Anshu Avatar answered Sep 30 '22 07:09

Avinash Kumar Anshu