Start a conversation

Calculating with grids: numbers

The instructions described in the article Calculate with grid fields  renders the objects in string (plain text). If you want to do mathematical operations with numbers, you will need to convert that text into numbers. For that you would use the parseInt() function. For example, we want to get the sum of the values in the first row of the grid shown below:

To get the sum of the first row (in this case it is 16), we would use:

parseInt(Object.values(obj[0])[0]) + parseInt(Object.values(obj[0])[1]); 

You cannot calculate with a (half-)empty grid field - all the cells that are used in the calculation have to be filled in the grid to be able to execute the calculation.


If the numbers contain decimals we will need to use the function parseFloat() instead. For example, we want to get the sum of the values in the first row of the grid shown below:

To obtain as result 29.5 we have used this code:

parseFloat(Object.values(obj[0])[0]) + parseFloat(Object.values(obj[0])[1]);
Choose files or drag and drop files
Was this article helpful?
Yes
No
  1. Castor Support Team

  2. Posted