This question already has answers here:
How to extract number from a string in javascript
(9 answers)
Get the first integers in a string with JavaScript
(5 answers)
How can I extract a number from a string in JavaScript?
(27 answers)
How to find a number in a string using JavaScript?
(9 answers)
Closed 2 years ago.
let string = "13th"
string = Number(string)
I expect it to return 13 instead I get NaN please how do I go about it
Try with parseInt()
If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. parseInt truncates numbers to integer values. Leading and trailing spaces are allowed.
let string = "13th";
string = parseInt(string);
console.log(string);
This question already has answers here:
How to convert decimal to hexadecimal in JavaScript
(30 answers)
Closed 3 years ago.
I wish to convert the whiteHex variable to a decimal using the parseInt() function, and store it in a variable, whiteDecimal.
var whiteHex = 'ffffff';
var whiteDecimal = parseInt(whiteHex);
I am unsure if the above is correct or not. The reason being, that I then wish to subtract 1 from whiteDecimal and store it in a variable offWhiteDecimal. This is where I am getting stuck. How can I subtract one from the ffffff hex value? Am I missing something within the parseInt function?
You're looking for this:
var whiteDecimal = parseInt(whiteHex, 16)
console.log(whiteDecimal - 1);
ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt#Syntax
This question already has answers here:
Parsing numbers with a comma decimal separator in JavaScript
(3 answers)
Closed 3 years ago.
I have an array like this:
["3.789,33", "2.702,17", "481,21", "1.149,44", "3.037,85", "44,24", "524,49", "1.378,42", "32,02"]
and I would like to convert it to an array of numbers/floats.
I tried .map(Number) and also parseInt() and parseFloat() but didn't seem to work out.
Can anybody help?
You need to replace . by empty string and , by . before parsing it to float
let arr = ["3.789,33", "2.702,17", "481,21", "1.149,44", "3.037,85", "44,24", "524,49", "1.378,42", "32,02"]
let final = arr.map(v=> parseFloat(v.replace(/\./g,'').replace(',','.')))
console.log(final)
This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 8 years ago.
I have a string that always contains 8 variable values that are separated with a hypen (-) like in the following example:
5-2-2-2-2-2-2-1
What is the best way to split this into 8 separate values so that I can use each of them in a variable if the values can be either an integer or the value 'n/a' ?
Many thanks for any help with this, Tim.
var str = '5-2-2-2-2-2-2-1';
var parts = str.split('-');
for (var i=0;i<parts.length;i++){
console.log(parts[i]);
}
You are searching for the String.split() method
This question already has answers here:
toString and valueOf truncates trailing 0s after decimal
(2 answers)
Closed 9 years ago.
I have a number, 2.0e-14, and I want to convert it to string. I'm using (2.0e-14).toString(), and what I get is the string 2e-14, because it's removing the zeroes at the right.
Is there any way of keep the zero at the right than to manipulating the string?
Use the Number toPrecision method.
(2.0e-14).toPrecision(2)