0

I want to remove the number to the left side of the decimal if that digit is less than 1. Basically, I want to remove a zero if its there.

Meaning 0.25 would become .25 but 1.50 would remain 1.50

$(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]);
    }
  })
});
Nick T
  • 643
  • 2
  • 9
  • 22

1 Answers1

0

You can use a simple regex

^0+(?=\d*\.\d+)
  • ^ - start of string
  • 0+ - Match 0 one or more time
  • (?=\d*\.\d+) - Match must be followed by digit zero or time and a decimal and one or more digits after it

let nums = ["0.25","1.25","001.23",".23",'0.0']

nums.forEach(value=>{
  value = value.replace(/^0+(?=\d*\.\d+)/,'')
  console.log(value)
})
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • @NickT you can change value to this format before `$(table.column(1).footer()).text(totals[0]);` and then pass the values – Code Maniac Jun 01 '19 at 13:56
  • I should be more clear. I don't really know JavaScript. I'm basically just using it for a tiny function of my website. Can you be more specific please. – Nick T Jun 01 '19 at 15:10
  • Can anybody help with the specifics please? – Nick T Jun 02 '19 at 19:52