Ignore Zero When Rendering a Number in Money Format
Ignore Zero When Rendering a Number in Money Format

To display currency, I am using:
columnDefs: [
{ targets: [ 1 ], render: $.fn.dataTable.render.number( ',', '.', 2, '$','' ) },
],
It works great! My numbers are displayed as $136,029.14.
However, when a number is 0, I want to show my users 0, not $0.00. How can I modify my code in columnDefs to do that? My first thought was a ternary statement but without knowing the value of the cell, I am unsure how to proceed.
This question has an accepted answers - jump to answer
Answers
Use
columns.render
. The number renderer,$.fn.dataTable.render.number
, can take an additional APIdisplay()
. Something like this should work:Didn't test this code so hopefully its close enough to make work
Kevin
Thank you, kthorngren. Your suggestion worked, WITH some modifications. Here's the final code I ended up using, hopefully it helps someone in the future:
$( '#certificates-datatable' ).DataTable( { scrollX: true, pageLength: 50, pagingType: 'full_numbers', 'order': [ [ 4, 'desc' ] ],
columnDefs: [
{ targets: [ 1 ], render: function( data, type, row ) {
return ( data === '0' || data === '0.00' ) ? '0' : $.fn.dataTable.render.number( ',', '.', 2, '$', '' ).display( data ) }
},
]
} );
I think this is a really good point actually. I think this is something that should be built into the number renderer. I'll add that for the next release.
Allan