+ 2
How to convert a string to number?
Ex. var a = 1.1.06 How can the string be converted to number? Also, how can I altogether convert the strings in an array into numbers? ex . array = [1.0.5, 1.1.2, 1.2.5] Into(expected output) array = [105, 112, 125]
9 Respuestas
+ 9
1.1.06 is not a valid numeric value. Even if you try, converting it from a string "1.1.06" to a number, you most likely will get a NaN (not a number).
(Edit)
If you use parseInt() then you get 1 (one) only. Any character following the '.' will be ignored.
+ 9
Pradnya Meshram ,
can you please give a sample what you expect as output when using "1.1.06" as input?
+ 8
it can be also be one by using split(), then join() and finally convert the result to int:
var myString = '1.1.06';
var n = parseInt(myString.split(".").join(""));
console.log(n)
+ 5
You can use the String method replace/replaceAll wrapped in parseInt method.
ex: let test = '1.1.06'
let num = parseInt(test.replaceAll(/\D/,""))
+ 2
Lothar
I'm not sure if it's possible, but I'm expecting an output removing the dots
ex. If the value is 1.1.06
I want the output as 1106
0
Please can you explain this in a simple syntax
0
Var a="25";
Number(a);
0
Take a look at this.
const names = ["David", "Veronica"];
const nameLengths = names.map(function (name) { return name.length;
});
nameLengths; //output [5,8]