2 decimal & end by 0

2 decimal & end by 0

lenamtllenamtl Posts: 265Questions: 65Answers: 1

Hi,

Why the latest 0 of 2 decimal value does not display?
50.50 will be displayed as 50.5

I'm filling the table using Ajax.
var dataSet = [
[ "Mydata", 52215, "95%", 50.50, -4.3, 7.2, "02" ],
]

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,770

    Use a number renderer to define the output format.

    Kevin

  • lenamtllenamtl Posts: 265Questions: 65Answers: 1

    Hi,

    How can I apply it to my actual code?

            var dataSet = [
                [ "Mydata", 52215, "95%", 50.50, -4.3, 7.2, "02" ],
                [ "Mydata", 52216, "90%", 50, -4.3, 7.2, "02" ],
            ]
    
    
    var table1 = $('#stat').DataTable({
        data: dataSet1,
    
        "columns": [
                { "title": "test"},
                { "title": "test1"},
                { "title": "test2"},
                { "title": "test3"},                        
                { "title": "test4"},
                { "title": "test5"},
                    { "title": "test6"},                        
            ],
    
                ..........
    });
    
  • allanallan Posts: 61,726Questions: 1Answers: 10,110 Site admin
    edited March 2021
    {
      title: "test"
      render: $.fn.dataTable.render.number( ',', '.', 2)
    }
    

    Allan

  • lenamtllenamtl Posts: 265Questions: 65Answers: 1

    how can I get If number does not have decimal don't add 00
    but if it have decimal show 2

    50.50 will show 50.50
    56 will show 56 not 56.00

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,770

    See this thread with an example doing something similar.

    Kevin

  • lenamtllenamtl Posts: 265Questions: 65Answers: 1
    edited March 2021

    Hi,

    let say I wan to check if integer no sure if this could work
    var result = (n - Math.floor(n)) !== 0;
    so how to apply if to a specific column

     {
            title: "test"
             render: $.fn.dataTable.render.number( ',', '.', 2)
     }
    
  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,770

    let say I wan to check if integer no sure if this could work

    Check this SO thread for ways to check for an integer.

    so how to apply if to a specific column

    Like this:

        "columns": [
                { "title": "test",
                render: $.fn.dataTable.render.number( ',', '.', 2)
                },
                { "title": "test1"},
                { "title": "test2"},
                { "title": "test3"},                       
                { "title": "test4"},
                { "title": "test5"},
                    { "title": "test6"},                       
            ],
    

    Kevin

  • lenamtllenamtl Posts: 265Questions: 65Answers: 1

    Sorry for the confusion

    I know where to put the the render = it's working not like I expected but it working.
    render: $.fn.dataTable.render.number( ',', '.', 2)

    Now how can I apply the IF only on
    "title": "test",
    Not on all the column
    All examples are based on data not on specific column

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,770

    Did you look at the example in the thread I linked? You just need to change the if condition to match your requirements.

    Kevin

  • lenamtllenamtl Posts: 265Questions: 65Answers: 1
    edited March 2021

    Hi yes I have checked the example, but I'm not sure if I should use

    render: function( data, type, row )

    Is this the correct way to use it with my datatset?

    var dataSet = [
                [ "Mydata", 52215, "95%", 50.50, -4.3, 7.2, "02" ],
                [ "Mydata", 52216, "90%", 50, -4.3, 7.2, "02" ],
            ]
    
    "columns": [
                { 
            "title": "test1",
            render: function( data, type, row ) {
                if ( data <= 100 ) {
                    return $.fn.dataTable.render.number( '', '.', 2, '', '%' ).display(data);
                } else {
                    return $.fn.dataTable.render.number( ',', '.', 0 ).display(data);
            }
                    { "title": "test2"},
            }           
        },                  
    ],
    
  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,770
    Answer ✓

    but I'm not sure if I should use ** data**

    Take a look at the columns.render docs to learn what the parameters mean. IF you want to check the value in that cell use data. If you want to check the value in another cell of the row use row with the column index, for example row[1] to get the second column.

    Is this the correct way to use it

    Looks correct. Did you try it?

    Kevin

This discussion has been closed.