10

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.

passion
  • 1,000
  • 6
  • 20
  • 47

4 Answers4

13

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);
Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
  • i updated my question, i currently have problem integrating your response. – passion Jan 18 '17 at 21:58
  • @marcel Assigning `Number.MIN_SAFE_INTEGER` to it will not cause any problems. You're just re-assigning `mynumber` to a different value, presumably. Please create a [minimal, complete and verifiable example](stackoverflow.com/help/mcve) of your problem and I'd be more than happy to help you solve it. If you need something to help you quickly hash it out, [JSFiddle](https://jsfiddle.net/) is great for that sort of thing. – Mike Cluck Jan 18 '17 at 22:12
2
  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 );
Abhinav Galodha
  • 9,293
  • 2
  • 31
  • 41
  • i updated my question, i can't succeed applying your solution – passion Jan 18 '17 at 21:58
  • The `Number.MIN_SAFE_INTEGER` constant represents the minimum safe integer in JavaScript. If your myNumber is assigned that value, then why are you converting it again to number ? In case, you are overriding the value from number to a string, then also it should work. I updated the answer as per your comment. – Abhinav Galodha Jan 18 '17 at 22:00
  • the problem is that not all of the instances of `mynumber` has space, so this solution breaks when there is not. what should i do then? – passion Jan 18 '17 at 22:10
  • which string value is it failing? this solution should work for any numbers – Abhinav Galodha Jan 18 '17 at 22:29
1

You can replace all white space with replace function

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

OutPut is 120000

0

Just like playing with javascript :-)

var number= '120 00'; 
var s= '';
number = parseInt(number.split(' ').join(s), 10);
alert(number);
Muhammad Usman
  • 10,039
  • 22
  • 39