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 OverflowAssuming 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)
\d matches only digits, it's the short for [0-9]. For example, in 6.35 \d+ matches 6 and then 35 separately and the dot is ignored. What you get in result is array containing those matches.
As suggested in other answers, use of match is redundant in your case and you can go with:
array.map(Number)
You can use Array.prototype.reduce method for this:
var result = ["0 1 0", "1 -1 0", "-1 -1 0"].reduce(function(prev, curr) {
return prev.concat(curr.split(' ').map(Number));
}, []);
alert(result); // [0, 1, 0, 1, -1, 0, -1, -1, 0]
Instead of .map(Number) you can use .map(parseFloat) of course if you need.
Or even shorter:
var result = ["0 1 0", "1 -1 0", "-1 -1 0"].join(' ').split(' ').map(Number);
You could do something like this.
var res = []
for (var y = 0; y < vertices.length; y++) {
var temp = vertices[y].split(" ");
for (var i = 0; i < temp.length; i++) {
res.push(parseFloat(temp[i]));
}
}
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);
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)
It's as you say. parseFloat() considers as float numbers only strings with . character, not ,. Also it returns NaN for null values:
var arr = ["40,78","6,27","0,00",null,null,null,null,null,null,null,null,null,"35,87",null,null];
var res = arr.map(function(val) {
return val ? parseFloat(val.replace(',', '.')) : null;
});
console.log(res);
Solution:
function MyCtrl($scope) {
$scope.array= ["40,78","6,27","0,00",null,null,null,null,null,null,null,null,null,"35,87",null,null]
for(var i=0; i<$scope.array.length; i++) {
if ($scope.array[i] !== null) {
$scope.array[i] = parseFloat($scope.array[i].replace(',','.'))
}
}
}
Link: http://jsfiddle.net/p2kpeptq/1/
Try parseFloat("28.489084691371996") function. It will help you.
parseFloat("28.489084691371996")
Let me know if you need further help.
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)));
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)
You could use a sequence of operations:
- replace "," with ""
- parseFloat()
- toFixed(2)
(1)
var dataSum = "";
You are initializing dataSum as a string. For strings, the += is a concatenation operator, so you'll get 100200300400500 because of concatenation. You should initialize it to 0:
var dataSum = 0;
(2)
parseFloat does not modify the input parameter. The float value is returned. So you should use
dataSum += parseFloat(data[i]);
(3)
var data = [];
data = dataString.split(",");
The 2nd assignment will override the 1st. Just write
var data = dataString.split(",");
(BTW, ECMAScript 5 supports this one-liner:
return "100,200,300,400,500".split(/,/).map(parseFloat).reduce(function(x,y){return x+y;})
)
parseFloat returns a value, which you should use to make your calculation, also, you should initialize your dataSum variable to a numeric value.
var dataSum = 0;
for (var i=0; i < data.length; i++) {
var current = parseFloat(data[i]);
dataSum += current;
// to save back onto array
// data[i] = current;
}
Make necessary modfications. Refer Float64Array API .You define length in Float64array as a parameter.
your length is coming 1.
var num = "65";
var x = new Float64Array(1);
x[x.length-1] = parseFloat(num);
console.log(x[0]);
Float64Arrays can't grow and shrink like ordinary JS arrays. You have to allocate all the elements when creating it:
var sym = new Float64Array(10);
You can then assign elements:
sym[0] = 123.456;
console.log(sym);
> [123.456, 0, 0, 0, 0, 0, 0, 0, 0, 0]
First, you have to convert a string into a buffer, and then create a Float32Array on that buffer. Optionally, spread it to create a normal Array:
str = '\x00\x00\x80?\x00\x00\x00@'
bytes = Uint8Array.from(str, c => c.charCodeAt(0))
floats = new Float32Array(bytes.buffer)
console.log(floats)
console.log([...floats]);
Floating-point representation can vary from platform to platform. It isn't a good idea to use a binary serialized form of a floating-point number to convey a number from one machine to another.
That said, the answers to this question might help you, provided the encoding used by the JavaScript matches that of the source of the numbers:
Read/Write bytes of float in JS
If they're meant to be separate values, try this:
var values = "554,20".split(",")
var v1 = parseFloat(values[0])
var v2 = parseFloat(values[1])
If they're meant to be a single value (like in French, where one-half is written 0,5)
var value = parseFloat("554,20".replace(",", "."));
Have you ever tried to do this? :p
var str = '3.8';ie
alert( +(str) + 0.2 );
+(string) will cast string into float.
Handy!
So in order to solve your problem, you can do something like this:
var floatValue = +(str.replace(/,/,'.'));
Instead of this
array[i] = parseFloat(array[i]);
try
array[i] = +array[i];
Above handles all the test cases pretty well.
Here is the working fiddle
Try this :
str = "[-0.9999, 1., 1.00000000000E-5,-1E5]";
str.slice(1, str.length-1).split(',').map(Number);
// [-0.9999, 1, 0.00001, -100000]