0

I have a couple of arrays of numbers that are in string format, like:

A = ["1046.7779999999038", "1106.244999999035", "833.5839999999735"]

I want to use the numbers in number format further for some calculations and need them in numeric format, so that I have an array of numbers.

I wanted to use parseInt, but this is not quite right it seems. Maybe I need to split my array first? Ideally I want a function that has an array as input and converts it to the same array in numeric format.

Can somebody help me with this?

asked Nov 20, 2014 at 0:00

6 Answers 6

3

You use the parseFloat function for parsing floating point numbers.

You can use the map method to parse all of the strings in the array into an array of numbers:

A = $.map(A, function(s){ return parseFloat(s); });
answered Nov 20, 2014 at 0:05
Sign up to request clarification or add additional context in comments.

6 Comments

.each() seems more appropriate here since you can just assign to the existing array rather than create a whole new array.
@jfriend00: If you want to do that then a plain loop would be more appropriate. You can't assign the parsed values back to the value that you get in the each callback, so you have to access the array by index anyway.
.each() or for are both fine and better than $.map() IMO.
@jfriend00: This kind of conversion is exactly what the map method is intended for, it's up to you if you don't want to use it.
@jfriend00: Why do you say that it doesn't do a conversion when it clearly does a conversion? I think that you are utterly confused...
|
2

You can call parseFloat() on each member of the array.

answered Nov 20, 2014 at 0:05

Comments

1

Use parseFloat:

function convert(arr){
 var ret=[];
 for(i in arr){
 ret.push (parseFloat(I));
 }
 return ret;
}
answered Nov 20, 2014 at 0:12

Comments

0

As others have said, parseFloat() is the conversion function you want to use. To convert your array in place without creating a new temporary array, you can either use a for loop or the .forEach() iterator to cycle through the array and convert the elements in place:

for (var i = 0; i < A.length; i++) {
 A[i] = parseFloat(A[i]);
}

or

A.forEach(function(value, index) {
 A[index] = parseFloat(value);
});
answered Nov 20, 2014 at 0:39

Comments

0

Let a separate array B store all your number conversions.

var convert = function() {
 for(var i=0;i<A.length< i++){
 B[i]=Number(A[i]);
 }
 }
answered Nov 20, 2014 at 0:06

Comments

0

Try

$.each(A, function(i, el) {
 A[i] = Number(el)
});

var A = ["1046.7779999999038", "1106.244999999035", "833.5839999999735"];
$.each(A, function(i, el) {
 A[i] = Number(el)
});
console.log(typeof (A[0] && A[1] && A[2]) === "number", A)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

answered Nov 20, 2014 at 0:39

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.