I would like to format the number in the footer of my table so that there is no 0 to the left of the decimal point when the number is less than 1.
-
0.50 would become .50
-
1.60 would remain 1.60
I have almost no experience with Javascript so if you could answer the question pertaining to my specific situation that would be helpful.
<div align="center">
<table id = 'mytabl' class="display compact nowrap">
<thead>
<tr>
<th>Number 1</th>
<th>Number 2</th>
</tr>
</thead>
<tbody>
{% for num in numberdata.mytable_set.all %}
<tr>
<td>{{ num.number1 }}</td>
<td>{{ num.number2 }}</td>
</tr>
{% endfor %}
</tbody>
<tfoot>
<tr>
<th></th>
<th></th>
</tr>
</tfoot>
</table>
<script>
$(document).ready(function() {
$('#mytabl').DataTable({
"searching": true,
"pageLength": 40,
"scrollX": true,
"paging": false,
"info": false,
drawCallback: () => {
const table = $('#mytabl').DataTable();
const tableData = table.rows({
search: 'applied'
}).data().toArray();
const totals = tableData.reduce((total, rowData) => {
total[0] += parseFloat(rowData[1]);
total[1] += parseFloat(rowData[2]);
return total;
}, [0, 0]);
$(table.column(1).footer()).text(totals[0]);
$(table.column(2).footer()).text(totals[1]);
}
})
});
</script>