jQuery – JavaScript split string to array of float

If we want to use JavaScript to split string to array of float, then we can simply use “split” method. For example if we have string in text box named textfield1 in our document form, we can simply convert data from the string into array of numbers by using single line of code in JavaScript.

var s_t = document.form1.textfield1.value.split(‘,’);

In the text box, numbers are separated by comma. See the running example.

If we want to do the same in jQuery, one way to split string to array of float is to use two arrays and three lines of following code:

var cosPoints = $(‘#textfield1’).val().split(‘,’);
var arrayPoints = [];
$.each(cosPoints, function(index, point) { arrayPoints.push(parseFloat(cosPoints[index])) })

Using jQuery split string to array of int we can use method parseInt(string, radix) instead of parseFloat.

External links:

Jquery split string to array of float on Stackoverflow
Jquery split string to array of float on w3schools
Jquery split string to array of int on developer.mozilla.org

Posted in jquery

Leave a Reply

Your email address will not be published. Required fields are marked *

*