+ 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]

28th Jul 2022, 4:56 AM
Pradnya Meshram
Pradnya Meshram - avatar
9 odpowiedzi
+ 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.
28th Jul 2022, 5:31 AM
Ipang
+ 9
Pradnya Meshram , can you please give a sample what you expect as output when using "1.1.06" as input?
28th Jul 2022, 5:36 AM
Lothar
Lothar - avatar
+ 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)
28th Jul 2022, 6:07 PM
Lothar
Lothar - avatar
+ 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/,""))
28th Jul 2022, 2:00 PM
ODLNT
ODLNT - avatar
+ 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
28th Jul 2022, 7:14 AM
Pradnya Meshram
Pradnya Meshram - avatar
+ 2
Thank you Ipang , Lothar , ODLNT .
28th Jul 2022, 6:48 PM
Pradnya Meshram
Pradnya Meshram - avatar
0
Please can you explain this in a simple syntax
29th Jul 2022, 5:06 PM
Bello Adedapo
Bello Adedapo - avatar
0
Var a="25"; Number(a);
29th Jul 2022, 5:37 PM
Seriaman Telaumbanua
Seriaman Telaumbanua - avatar
0
Take a look at this. const names = ["David", "Veronica"]; const nameLengths = names.map(function (name) { return name.length; }); nameLengths; //output [5,8]
25th Aug 2022, 4:29 PM
Bello Adedapo
Bello Adedapo - avatar