Change Datatable Decimal Place to Comma

Software Used:

  • Datatables 1.10.20
  • PHPMyAdmin

Table in PHPMyAdmin

Datatable Display Result:

Wanted:

My Client Wanted to formatting QTY column decimal places with comma and 2 digit after comma like image below:

Question:

How to do it … ?

Answer:

You can format the code like this :

$('#your_tbody_id').DataTable({
  "columnDefs": [
  { 
     targets: [1],"render": function ( data, type, row )
     {
        let angka=parseFloat(data.replace(/[^0-9]/g, '.')).toFixed(2);
	return angka.replace(/[^0-9]/g, ',')
     }
  }
		]
})

Explanation:

  • columnDefs is for column definition
  • targets: [1] is for targetting column with index 1
  • data.replace(/[^0-9]/g, ‘.’) is for replacing all character other than number to dot
  • parseFloat is for parsing data with dot decimal place to float if decimal place is comma then it will error
  • toFixed(2) is to make two digit after decimal place
  • angka.replace(/[^0-9]/g, ‘,’) is for replacing all character other than number to comma

Thanks for reading

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *