Assuming you have an array in a string format, you can use the following regex to match all the decimals and then use .map(Number)

const str = "['6.35', '2.72', '11.79', '183.25']",
      array = str.match(/\d+(?:\.\d+)?/g).map(Number)

console.log(array)

Answer from adiga on Stack Overflow
🌐
Dirask
dirask.com › posts › JavaScript-convert-string-array-to-float-array-DWkWBD
❤ 💻 JavaScript - convert string array to float array - Dirask
In this article, we would like to show you how to convert an array of strings to an array of numbers in JavaScript. Quick solution: See also References
Top answer
1 of 6
2

Try this:

JSON.parse(array.trim().replace(/\s+/g, ','));

const array = '[-0.20074345 0.17205992 0.10611761 -0.05400428 0.01761392 -0.02889173 -0.02301165 0.02264201 0.14304711 -0.01270375 0.12890713 -0.07083786 -0.16351621 -0.10163811 -0.00238512 0.11788289 -0.16991724 -0.18428595 -0.07542907 -0.1000075 0.0540689 0.02885393 -0.02544132 0.00992649 -0.14298916 -0.35953391 -0.04501984 -0.16696566 0.03482476 -0.12809309 -0.00494457 -0.03969483 -0.20044276 -0.02079746 -0.0428996 0.08433075 0.01925042 -0.05510717 0.1244439 -0.00250635 -0.09836875 -0.02853961 0.0347203 0.32151613 0.15131931 0.03381695 -0.01653017 -0.02396783 0.05017283 -0.15214115 0.09659076 0.11705139 0.06577846 0.08330107 0.10685007 -0.12960862 0.03072787 -0.04441368 -0.23768349 0.00972465 -0.01230429 0.01334048 -0.08945481 -0.05727607 0.23321879 0.1306915 -0.04987631 -0.16629016 0.19155866 -0.12205004 0.02197403 0.02836045 -0.07212473 -0.07092203 -0.22874205 0.06980464 0.36526302 0.08318733 -0.19271423 0.05348996 -0.12581617 -0.10200049 0.01825046 0.01343039 -0.16283956 0.11140806 -0.10517029 0.09450665 0.13496454 0.01246606 -0.05901476 0.08878441 0.01477766 -0.07605766 0.08017815 -0.02794793 -0.11664257 0.01248038 -0.14993706 -0.08350047 0.09859181 -0.10223893 -0.06312436 0.11509642 -0.15772626 0.10087827 0.1003866 -0.08574189 -0.06877069 0.10180675 -0.15567036 -0.0904673 0.14713711 -0.19140017 0.14492355 0.13951159 0.0426133 0.14236017 0.04866033 0.09195794 -0.02404636 0.03078505 -0.09961188 -0.03846828 0.03835503 0.07761194 0.05843619 0.10325849]';

const result = JSON.parse(array.trim().replace(/\s+/g, ','));

console.log(result);

2 of 6
0

You were close. Just try to remove the brackets from the string:

var array = '[-0.20074345 0.17205992 0.10611761 -0.05400428 0.01761392 -0.02889173 -0.02301165 0.02264201 0.14304711 -0.01270375 0.12890713 -0.07083786 -0.16351621 -0.10163811 -0.00238512 0.11788289 -0.16991724 -0.18428595 -0.07542907 -0.1000075 0.0540689 0.02885393 -0.02544132 0.00992649 -0.14298916 -0.35953391 -0.04501984 -0.16696566 0.03482476 -0.12809309 -0.00494457 -0.03969483 -0.20044276 -0.02079746 -0.0428996 0.08433075 0.01925042 -0.05510717 0.1244439 -0.00250635 -0.09836875 -0.02853961 0.0347203 0.32151613 0.15131931 0.03381695 -0.01653017 -0.02396783 0.05017283 -0.15214115 0.09659076 0.11705139 0.06577846 0.08330107 0.10685007 -0.12960862 0.03072787 -0.04441368 -0.23768349 0.00972465 -0.01230429 0.01334048 -0.08945481 -0.05727607 0.23321879 0.1306915 -0.04987631 -0.16629016 0.19155866 -0.12205004 0.02197403 0.02836045 -0.07212473 -0.07092203 -0.22874205 0.06980464 0.36526302 0.08318733 -0.19271423 0.05348996 -0.12581617 -0.10200049 0.01825046 0.01343039 -0.16283956 0.11140806 -0.10517029 0.09450665 0.13496454 0.01246606 -0.05901476 0.08878441 0.01477766 -0.07605766 0.08017815 -0.02794793 -0.11664257 0.01248038 -0.14993706 -0.08350047 0.09859181 -0.10223893 -0.06312436 0.11509642 -0.15772626 0.10087827 0.1003866 -0.08574189 -0.06877069 0.10180675 -0.15567036 -0.0904673 0.14713711 -0.19140017 0.14492355 0.13951159 0.0426133 0.14236017 0.04866033 0.09195794 -0.02404636 0.03078505 -0.09961188 -0.03846828 0.03835503 0.07761194 0.05843619 0.10325849]';

Array.from(array.substring(1, array.length-1).split(" "), Number)
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-convert-string-into-float-in-javascript
How to convert string into float in JavaScript? | GeeksforGeeks
June 21, 2024 - In this method, we will use the parseFloat() method which is an inbuilt function in JavaScript that is used to accept the string and convert it into a floating point number.
Top answer
1 of 5
2

Try parseFloat("28.489084691371996") function. It will help you.

parseFloat("28.489084691371996")

Let me know if you need further help.

2 of 5
1

DEMO

var jsonObj = [{"temperSensorData":"28.489084691371996","temperSensorUnit":"celsius","timestamp":"1493270171424","timestamp2":"1493270171454","timestamp3":"1493270171454","val":"0"},{"temperSensorData":"28.48908469137112","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171522","timestamp3":"1493270171523","val":"1"},{"temperSensorData":"28.489084691371186","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171523","timestamp3":"1493270171524","val":"2"},{"temperSensorData":"28.489084691371595","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171524","timestamp3":"1493270171525","val":"3"},{"temperSensorData":"28.48908469137168","temperSensorUnit":"celsius","timestamp":"1493270171428","timestamp2":"1493270171529","timestamp3":"1493270171529","val":"4"},{"temperSensorData":"28.489084691371684","temperSensorUnit":"celsius","timestamp":"1493270171428","timestamp2":"1493270171529","timestamp3":"1493270171529","val":"5"},{"temperSensorData":"28.489084691371357","temperSensorUnit":"celsius","timestamp":"1493270171429","timestamp2":"1493270171530","timestamp3":"1493270171531","val":"6"},{"temperSensorData":"28.489084691371144","temperSensorUnit":"celsius","timestamp":"1493270171429","timestamp2":"1493270171531","timestamp3":"1493270171532","val":"7"},{"temperSensorData":"28.48908469137181","temperSensorUnit":"celsius","timestamp":"1493270171431","timestamp2":"1493270171546","timestamp3":"1493270171546","val":"25"}];

console.log(jsonObj.map(item => parseFloat(item.timestamp)));

Top answer
1 of 4
1

Since you want a float, then i assume that it need to be programmable usable and don't even need to be human readable - cuz the only way you will get that is by keeping it as a string...

var data = [{"ID":"September-2016", "Product":"September-2016", "Poured":"111,759.07", "Sold":"107,660.97", "Loss":"-4,098.10", "Variance":"-3.67", "startDate":"2016-09-01", "endDate":"2016-09-22"}, {"ID":"November-2015", "Product":"November-2015", "Poured":"53,690.25", "Sold":"52,953.60", "Loss":"-736.65", "Variance":"-1.37", "startDate":"2015-11-20", "endDate":"2015-11-30"}, {"ID":"May-2016", "Product":"May-2016", "Poured":"156,401.65", "Sold":"151,192.51", "Loss":"-5,209.14", "Variance":"-3.33", "startDate":"2016-05-03", "endDate":"2016-05-31"}, {"ID":"March-2016", "Product":"March-2016", "Poured":"49,260.22", "Sold":"49,399.14", "Loss":"138.92", "Variance":"0.28", "startDate":"2016-03-01", "endDate":"2016-03-09"}, {"ID":"June-2016", "Product":"June-2016", "Poured":"162,126.88", "Sold":"161,718.62", "Loss":"-408.26", "Variance":"-0.25", "startDate":"2016-06-01", "endDate":"2016-06-30"}, {"ID":"July-2016", "Product":"July-2016", "Poured":"160,185.68", "Sold":"154,882.40", "Loss":"-5,303.28", "Variance":"-3.31", "startDate":"2016-07-01", "endDate":"2016-07-31"}, {"ID":"January-2016", "Product":"January-2016", "Poured":"355,509.26", "Sold":"179,696.72", "Loss":"-175,812.54", "Variance":"-49.45", "startDate":"2016-01-01", "endDate":"2016-01-31"}, {"ID":"February-2016", "Product":"February-2016", "Poured":"150,980.73", "Sold":"146,248.72", "Loss":"-4,732.01", "Variance":"-3.13", "startDate":"2016-02-01", "endDate":"2016-02-29"}, {"ID":"December-2015", "Product":"December-2015", "Poured":"167,843.42", "Sold":"163,732.95", "Loss":"-4,110.47", "Variance":"-2.45", "startDate":"2015-12-01", "endDate":"2015-12-31"}, {"ID":"August-2016", "Product":"August-2016", "Poured":"168,853.51", "Sold":"160,024.84", "Loss":"-8,828.67", "Variance":"-5.23", "startDate":"2016-08-01", "endDate":"2016-08-31"}]

data.forEach(function(item){
  item.Poured = parseFloat(item.Poured.replace(/,/g, ''))
  // item.Sold = parseFloat(item.Sold.replace(/,/g, ''))
  // item.Variance = parseFloat(item.Variance.replace(/,/g, ''))
})

console.log(data)

2 of 4
0

You could use a sequence of operations:

  1. replace "," with ""
  2. parseFloat()
  3. toFixed(2)
Find elsewhere
🌐
TutorialKart
tutorialkart.com › javascript › javascript-convert-string-to-float
How to convert String to Float in JavaScript?
September 23, 2021 - To convert a string to float in JavaScript, call parseFloat() function and pass the string as argument to it.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Post.Byes
post.bytes.com › home › forum › topic › c sharp
How to convert from String array to float array? - Post.Byes
You'll want to run through the elements in the string array and convert them to a float... I'd suggest using float.TryParse( ...) to do the conversion as well in order to have your program handle strings that aren't actually floats. Have a look at the doc for the TryParse methods...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-convert-string-into-float-in-javascript
Converting string into float in JavaScript - GeeksforGeeks
February 25, 2026 - In this method, we will use the parseFloat() method which is an inbuilt function in JavaScript that is used to accept the string and convert it into a floating point number.
🌐
Favtutor
favtutor.com › articles › convert-string-to-float-javascript
Convert String to Float in JavaScript (5 Easy Ways)
November 30, 2023 - We finally check the type of the “num” variable to verify whether the Number() function has converted the string to a float number. We have obtained the desired result. In JavaScript, the unary plus operator (+) is a simple and concise way ...