lokopi lokopi - 3 months ago 42
jQuery Question

How to round to 2 decimal places in jquery datatable?

I am using jquery datatable aocolumns to round the specific columns to two decimal places but it seems like i am unable to get the correct regular expression for this or may be their is something wrong with my logic.

"aoColumnDefs": [ {
"aTargets": [ 7 ],
"mRender": function (data, type, full) {
var formmatedvalue = data.replace(/\d+(\.\d{1,2})?/, "")
return formmatedvalue;
}
}],


The output for that column should be

120.02
1560.56
565645.25
124995.89
etc .....


Any possible solution for this?

Answer

Use this function:

function (data, type, full) {
     return data.toString().match(/\d+(\.\d{1,2})?/g)[0];
}

You would be looking for matching the part you need and not to replace.