我如何 return 从一串数字中得到一个浮点数?
How can i return a float from an a string of numbers?
当我在控制台中执行 svg.getAttribute("d") 时,我得到一串数字:
"-0.05990783410138251,0.13594470046082952,-0.06912442396313362,0.14976958525345618,-0.07603686635944701,0.15668202764976957,-0.08755760368663595,0.16589861751152069,-0.09677419354838718,0.16820276497695852,-0.10599078341013829,0.16820276497695852,-0.12211981566820279,0.16820276497695852,-0.13594470046082957,0.16820276497695852,-0.14055299539170513,0.16820276497695852 ,-0.14516129032258068,0.16589861751152069,-0.1474654377880184
我想将其转换为浮点数,所以我做了 parseFloat(svg.getAttribute("d")) 但我得到的只是返回的第一个数字:
-0.05990783410138251
我怎样才能得到所有的号码?
谢谢
您可以先使用 split()
函数通过 ',' 拆分字符串,然后可以应用 parseFloat()
.
使用String.prototype.split()
and Array.prototype.map()
你可以得到一个浮点值数组如下:
const values = '-0.05990783410138251,0.13594470046082952,-0.06912442396313362,0.14976958525345618,-0.07603686635944701,0.15668202764976957,-0.08755760368663595,0.16589861751152069,-0.09677419354838718,0.16820276497695852,-0.10599078341013829,0.16820276497695852,-0.12211981566820279,0.16820276497695852,-0.13594470046082957,0.16820276497695852,-0.14055299539170513,0.16820276497695852,-0.14516129032258068,0.16589861751152069,-0.1474654377880184';
const floats = values.split(',').map(parseFloat);
console.log(floats);
您需要先将其转换为字符串数组,然后才能单独解析它们。像这样:
var str = svg.getAttribute("d");
var strNums = str.split(',');
var nums = strNums.map(function (num) {
return parseFloat(num);
});
const numArray = thatStringOfNumbers.split(',').map((num) => parseFloat(num))
当我在控制台中执行 svg.getAttribute("d") 时,我得到一串数字:
"-0.05990783410138251,0.13594470046082952,-0.06912442396313362,0.14976958525345618,-0.07603686635944701,0.15668202764976957,-0.08755760368663595,0.16589861751152069,-0.09677419354838718,0.16820276497695852,-0.10599078341013829,0.16820276497695852,-0.12211981566820279,0.16820276497695852,-0.13594470046082957,0.16820276497695852,-0.14055299539170513,0.16820276497695852 ,-0.14516129032258068,0.16589861751152069,-0.1474654377880184
我想将其转换为浮点数,所以我做了 parseFloat(svg.getAttribute("d")) 但我得到的只是返回的第一个数字:
-0.05990783410138251
我怎样才能得到所有的号码?
谢谢
您可以先使用 split()
函数通过 ',' 拆分字符串,然后可以应用 parseFloat()
.
使用String.prototype.split()
and Array.prototype.map()
你可以得到一个浮点值数组如下:
const values = '-0.05990783410138251,0.13594470046082952,-0.06912442396313362,0.14976958525345618,-0.07603686635944701,0.15668202764976957,-0.08755760368663595,0.16589861751152069,-0.09677419354838718,0.16820276497695852,-0.10599078341013829,0.16820276497695852,-0.12211981566820279,0.16820276497695852,-0.13594470046082957,0.16820276497695852,-0.14055299539170513,0.16820276497695852,-0.14516129032258068,0.16589861751152069,-0.1474654377880184';
const floats = values.split(',').map(parseFloat);
console.log(floats);
您需要先将其转换为字符串数组,然后才能单独解析它们。像这样:
var str = svg.getAttribute("d");
var strNums = str.split(',');
var nums = strNums.map(function (num) {
return parseFloat(num);
});
const numArray = thatStringOfNumbers.split(',').map((num) => parseFloat(num))