Rounding values in a particular column

Rounding values in a particular column

trashbattrashbat Posts: 2Questions: 1Answers: 0

Is there a way to force values in a given column to be displayed with rounding? Currently I'm iterating through my dataset and calling Math.round() on the values before passing it to the table, which doesn't seem like the optimal solution.

This question has an accepted answers - jump to answer

Answers

  • ThomDThomD Posts: 334Questions: 11Answers: 43
    Answer ✓

    For display of a field, I use this

    columnDefs: [
        { render: function ( data, type, row ) {
                return (100 * data).toFixed(2) + "%";
                 }
            targets: [ 10 ] }
    ] 
    

    For rounding user edits (and data validation) before data submission, I use this

    dtEditor.on('preSubmit', 
        function ( e,o,action ){
            if  (isNaN(o.data.Merit2015)) {
                this.error('Merit2015',"Merit must be a number");
                return false;
            }
            else if (o.data.Merit2015 < 1 ) {
                this.error('Merit2015',"Merit must be more than $1");
                return false;
            }
            o.data.Merit2015 = (100 * Math.round(parseInt(o.data.Merit2015) / 100));    //round to nearest 100
            return true;
        } 
    );
    
    
  • trashbattrashbat Posts: 2Questions: 1Answers: 0

    Exactly what I was looking for, thanks! :)

This discussion has been closed.