Hello reader's in this tutorial we will convert string in a table to a numbers, for doing this we have to access the values in a HTML table and for processing we have to convert into numbers.
Using the Document Object Model (DOM) API we can Access the numbers and to convert the strings to number values we can use the global function parseInt:
Syntax:-
var rows = document.getElementById("table1").children[0].rows;
var numArray = new Array();
for (var i = 0; i < rows.length; i++) {
numArray[numArray.length] = parseInt(rows[i].cells[1].firstChild.data);
}
There are two arguments in the parseInt global function these are a required numeric string, and an optional radix (base).
For decimal it is assumed to be 10, if the radix is not provided.
It will return NaN if the provided string have not contains any number. The parser will change over the number up to the point where a non numeric quality is come to, and give back the outcome if the string contains a halfway number .
Syntax:-
var numString = "133 hectares";
var numHectares = parseInt(numString); // returns 133
when floating point format reaches the decimal and the integer part of the number is returned if the floating-point format is in the number.
In the event that the string could contain a floating point number and you need the outcome to be a drifting point number, utilize the parse Float worldwide capacity:
var numString = "1.458 hectares";
var fltNum = parseFloat(numString); // returns 1.458
Hope this will help you to understand.
0 Comment(s)