javascript parseInt to remove spaces from a string - javascript

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.

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);

Remove all whitespaces inside string by a replace function.
using the + 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

Just like playing with javascript :-)
var number= '120 00';
var s= '';
number = parseInt(number.split(' ').join(s), 10);
alert(number);

Related

how can I remove a variable amount of characters from the end of javascript string

I've been trying to get something like this to work but it just won't:
var str = str.slice(0, -"variable");
I need to remove a variable amount of characters from the end. thanks!
Use slice:
var str = "Hello, World";
var chars = 7;
var sliced = str.slice(0, -chars);
console.log(sliced);
This -"variable" is a coercion logic, so basically you're executing something as follow: str.slice(0, NaN);
console.log(-"variable"); // js engine is trying to convert to number
Just use the variable, you don't need to wrap it with quotes like a string.
var variable = 5;
var str = "EleFromStack".slice(0, -variable);
console.log(str)

How to convert string (number with prefix) to Double/Float in JS

How to convert a number with prefix into double/float e.g. STA01.02to 1.02?
Use regex to strip the non-numbers (excluding ".") for a more flexible solution:
parseFloat("STA01.02".replace(/[^0-9\.]+/g, ''));
// Assumed "STA0" is the fixed-length prefix, you can adjust the substring at the start you're getting rid of.
var myString = "STA01.02";
var noPrefix = myString.substring(4); // Just "1.02"
var myNumber = parseFloat(noPrefix);
console.log(myNumber); // Prints 1.02
If the prefix always the same...
var str = "STA01.02";
var number = parseFloat(str.substring(3));

javascript : parse float with string prefix in both start and end

how to convert this string to float?
i want result 900.50
case-1: var convertThis = 'any string here 900,50 also any string here';
case-2: var convertThis = 'any string here 900.50 also any string here';
How to do this?
Try following code:
var text = 'any string here 900,50 also any string here';
var matched = text.match(/\d+[,.]\d+/)[0].replace(',', '.');
var num = parseFloat(matched, 10);
console.log(matched);
console.log(num);
prints:
900.50
900.5
You could do this :
var num = parseFloat(convertThis.replace(/[^\d\.,]/g,'').replace(/,/,'.'));
But be aware that this would break as soon as you have more than one number or dot in your text. If you want something reliable, you need to be more precise about what the string can be.
Supposing you'd want to extract all numbers from a more complex strings, you could do
var numbers = convertThis.split(/\s/).map(function(s){
return parseFloat(s.replace(',','.'))
}).filter(function(v) { return v });
Here, you'd get [900.5]
var myFloat = +(convertThis.match(/\d+[,\.]?\d+/)[0].replace(",","."))

adding numbers from array?

i have:
var str="100px";
var number = str.split("px");
number = number[0];
var km = "100px";
var numberk = km.split("px");
numberk = numberk[0];
var gim = numberk+100;
var kim = number+100;
var fim = number+numberk;
document.write(gim+'<br>'+kim+'<br>'+jim+'<br>');
i would be thankfull if someone could me answere why the result are added like string rather than nummerical number in javascript i have used the isNaN(); function which shows this as a legal number. So how can this problem be solved.
thanks.
You could use the parseInt function in order to convert the string returned when spliting into integer:
number = parseInt(number[0], 10);
numberk = parseInt(numberk[0], 10);
Now the 2 variables are integers and you could perform integer operations on them.
You need to put parseInt() around each number before you use it. In fact, you could do this without removing the "px".
gim = parseInt(km) + 100;
simplest way to do this, you don't need to use split.
var str="150px";
var str1 = (parseInt(str)+100)+"px";
alert(str1);
OUTPUT:
200px
fiddle : http://jsfiddle.net/Kk3HK/1/
use parseInt()
var number = parseInt(str, 10);
var numberk = parseInt(km, 10);
Use parseInt to convert the string to a number.
var str = "100px";
var number = parseInt(str, 10);
parseInt stops when it finds the first non-number character, so you don't even need to remove the "px".
Wrap the numbers in parseInt().

How to save a removed string in a new string

I have this function :
string = string.replace(/^.*?([a-zA-Z])/, '$1');
and I'd like to save both strings : the one after the expression and the one removed.
How can I do it?
<script type="text/javacript">
var str = '44234lol';
var parts = str.split(/([a-zA-Z]+)/);
alert(parts[0]);
alert(parts[1]);
</script>
This would show what was removed from the original string and what you're left with (I've altered your regex but you could use the same technique) -
var portionremoved;
var string = '1234GF'
string = string.replace(/(\d+)([A-Z]+)/,function (removed,first,second) {
portionremoved = first;
return second;
});
alert(portionremoved);
alert(string);
string1 = string.replace(/^.*?([a-zA-Z])/, '$1');
Note that string.replace returns the replaced string while string still holds the previous value.

Categories