xxxxxxxxxx
var x = parseInt("1000", 10); // you want to use radix 10
// so you get a decimal number even with a leading 0 and an old browser ([IE8, Firefox 20, Chrome 22 and older][1])
xxxxxxxxxx
var myInt = parseInt("10.256"); //10
var myFloat = parseFloat("10.256"); //10.256
xxxxxxxxxx
const stringNumber = "42";
const number = Number(stringNumber);
console.log(typeof number); // Output: "number"
xxxxxxxxxx
let num = Number("123"); // 123
let num = Number("123.45"); // 123.45
let num = Number("blah"); // NaN (not a number)
xxxxxxxxxx
var myString = "869.99"
var myFloat = parseFloat(myString)
var myInt = parseInt(myString)
xxxxxxxxxx
// Converting a String into a Number in Javascript
// Longhand:
const num1 = parseInt("100");
const num2 = parseFloat("100.01");
console.log(num1);
console.log(num2);
// Shorthand:
const num3 = +"100"; // converts to int data type
const num4 = +"100.01"; // converts to float data type
console.log(num3);
console.log(num4);
xxxxxxxxxx
// String to Int
myStringInt = "10";
console.log(parseInt(myStringInt)); // expected result: 10
// String to Float
myStringFloat = "8.33";
console.log(parseFloat(myStringFloat)); // expected result: 8.33
// String to any numeric type
console.log(Number(myStringInt)); // expected result: 10
console.log(Number(myStringFloat)); // expected result: 8.33
xxxxxxxxxx
var str = "42"; // example string
var num = parseInt(str); // convert string to integer
console.log(num); // output: 42
var floatStr = "3.14"; // example string with floating-point value
var floatNum = parseFloat(floatStr); // convert string to floating-point number
console.log(floatNum); // output: 3.14