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
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.
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.
JavaScript String trim() The trim() method removes whitespace from both sides of a string. The trim() method does not change the original 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 .
We can use replace() to remove all the whitespaces from the string. This function will remove whitespaces between words too.
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);
+
operator convert the string to number.var mynumber = Number.MIN_SAFE_INTEGER;
mynumber = "120 000";
mynumber = mynumber.replace(" ", "");
console.log(+mynumber );
You can replace all white space with replace function
var mynumber = "120 000";
console.log(mynumber.replace(/ /g,''));
OutPut is 120000
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With