javascript variable 轉成 integer:
let float = parseInt(string, 10);
javascript variable 轉成 float:
let float = parseFloat(string);
javascript variable 轉成 numeber:
let float = new Number(string);
除此之外, 還有2個神奇的操作:
https://stackoverflow.com/questions/33544827/convert-string-to-either-integer-or-float-in-javascript
Multiply string which has number data with 1. You will get the Numeric data value.
var int_value = "string" * 1;
In your case
a = '2' * 1; // => parse to integer
b = '2.1' * 1; // => parse to float
c = '2.0' * 1; // => parse to float
d = 'text' * 1; // => don't parse //NaN value
For last one you will get NaN
value. Manually handle for NaN value.
以及使用 eval():
https://www.tutorialspoint.com/how-to-convert-string-into-float-in-javascript
let string = "3.456";
let float = eval(string);