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
  • 543
  • 1
  • 7
  • 19
  • number.replace(/^0+/, '') – gaetanoM Jun 01 '19 at 13:40
  • 1
    Do this where you render it, as a string. You could use a simple regex, such as `/^0+/` – Lissy93 Jun 01 '19 at 13:41
  • The first answer on the question linked is actually wrong for that question but right for this one i.e. they asked for `0.22` and you for `.22` – Mark Schultheiss Jun 01 '19 at 14:32
  • I hardly know the basics of JavaScript. So far up to this point its been trial and error and asking questions on here. How would I implement this for my particular situation. – Nick T Jun 01 '19 at 14:45

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
  • 35,187
  • 4
  • 31
  • 54
  • @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