Adding the values of column together to display to another column

Adding the values of column together to display to another column

bobs64956bobs64956 Posts: 18Questions: 6Answers: 0
                {
                    "data": null,
                    "render": function(data,type,row) { return (data["valueA"] + data["valueB"])} //Try to add these value
                },

When trying to use the "+" symbol, nothing happens and only show the valuesA and valueB like this (16.0016.00)
When using the (* and -) symbol, it works as intended. Was wondering if there was a way to add them together

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 10,722Questions: 25Answers: 2,449

    symbol

    Not sure what you mean.

    Looks like data["valueA"] and data["valueB"] are being treated as stirngs. You can convert them. Try this:

    return ( Number(data["valueA"] ) + Number(data["valueB"]) )

    Kevin

  • bobs64956bobs64956 Posts: 18Questions: 6Answers: 0

    I've tried the suggestion above. The ouput comes out as NaN .
    In terms of symbol, this is what I mean. As seen in the screenshots below (Both valueA & valueB are 16). When trying to do minus & multiply, they output both as 256 & 0. But when trying to add them together, it just output as "16.0016.00"

    Addition +

                    {
                        "data": null,
                        "render": function(data,type,row) { return (data["valueA"] + data["valueB"])}
                    },
    

    Minus -

                    {
                        "data": null,
                        "render": function(data,type,row) { return (data["valueA"] + data["valueB"])}
                    },
    

    Multiply *

                    {
                        "data": null,
                        "render": function(data,type,row) { return (data["valueA"] + data["valueB"])}
                    },
    

  • tangerinetangerine Posts: 2,775Questions: 22Answers: 325
    edited September 19

    16.0016.00
    That is string concatenation.

    I don't understand your examples. The code is the same in each of them.

    If you are sure the data type is string, try parseInt().
    https://www.w3schools.com/jsref/jsref_parseInt.asp

  • bobs64956bobs64956 Posts: 18Questions: 6Answers: 0
                columns: [
                    {
                        data:'valueA',
                    },
                    {
                        data:'valueB',
                    },
                    {
                        "data": null,
                        "render": function(data,type,row) { return (data["valueA"] + data["valueB"])}
                    },
                    {
                        defaultContent: '<input type="button" class="Edit" value="Edit"/><input type="button" class="Delete" value="Delete"/>'
                    },
    
                ]
    
    

    I've made a mistake in the above comment as the opeartors are not correct.
    This is the code for the table, trying to get the sum of the 2 columns and output it in another column

  • kthorngrenkthorngren Posts: 10,722Questions: 25Answers: 2,449

    This example shows what tangerine and I are talking about:
    http://live.datatables.net/hekubina/1/edit

    Both a and b are strings. Multiplying them works but adding them doesn't unless you convert them.

    If you need help with this then please post a link to your page or a test case so we can help debug. Otherwise you can use console.log and typeof to debug the data types for data["valueA" and data["valueB". Or use the browser's debugger.

    Kevin

  • kthorngrenkthorngren Posts: 10,722Questions: 25Answers: 2,449

    I've tried the suggestion above. The ouput comes out as NaN .

    What was your return statement when you got this result?

    Kevin

  • bobs64956bobs64956 Posts: 18Questions: 6Answers: 0
                    {
                        "data": null,
                        "render": function(data,type,row) { return (Number["valueA"] + Number["valueB"])}
                    },
    

    This is the return statement. Its kinda difficult to show the program as it is reading from a DB, so you wouldn't be able to see the full program. Below is something similar to what I want, but, Value A & B are coming from a DB table. Is there a way to change the data type to number?

    |Value A    |Value B    |Value C
    |     3     |     5     |     8     |
    |     4     |     2     |     6     |
    |     5     |     3     |     8     |
    
  • allanallan Posts: 52,724Questions: 1Answers: 8,070 Site admin
    Answer ✓
    return parseFloat(data['valueA']) + parseFloat(data['valueB']);
    

    is what you want.

    Allan

  • bobs64956bobs64956 Posts: 18Questions: 6Answers: 0

    Yep that did the trick, thanks allan :)

Sign In or Register to comment.