Render number to only required decimal places?

Render number to only required decimal places?

RyanG94RyanG94 Posts: 11Questions: 3Answers: 0

Hi, i am using the render.number function to display table figures with the relevant number of decimal places. Currently by using this line
{ data: "Discount", title: "Discount", className: "dataTable-progress editable", render: $.fn.dataTable.render.number(',', '.', 2, '', '%') },

The discount will display as 20.00% for example, however....is it possible to change the function to only show relevant figures after the decimal point....for example 20%, But in some cases 17.25% ?

i had a look through the forums and seen a similar case here https://datatables.net/forums/discussion/comment/216596? but i couldnt see if a solution had been reached.

Thank you for your help.

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406
    edited April 2023

    You could use a custom renderer, e.g. like this:

    { data: "Discount", title: "Discount", className: "dataTable-progress editable", 
      render: function (data, type, row) {
          var nR = $.fn.dataTable.render.number( ',', '.', 2 ).display;
          return nR(data).replace('.00', '') + '%';
      }
    
  • RyanG94RyanG94 Posts: 11Questions: 3Answers: 0

    Thank you for your response.

    This has worked great, further on from that i also have a Quanity column which can hold up to 4 decimal places.

    **data: "Quantity", title: "Qty", type: 'integer', className: "dt-head-right dt-body-right", render: $.fn.dataTable.render.number(',', '.', 4, '') **

    Is is possible to have a simialr result for this field ? it can potentially be 12.1234,
    12.5200 or 12.0000

    Sorry if this is a simple tweak to what you have suggested, i am fairly new to Jquery.

  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406
    edited April 2023
    { data: "Quantity", title: "Qty", type: 'integer', className: "dt-head-right dt-body-right",
      render: function (data, type, row) {
          var nR = $.fn.dataTable.render.number( ',', '.', 4 ).display;
          var rendered = nR(data).replace('.0000', '');
          if ( rendered.slice(-2) === '00' && rendered.indexOf('.') >= 0 ) {
              return rendered.slice(0, -2);
          }
          return rendered;
      }
    
  • RyanG94RyanG94 Posts: 11Questions: 3Answers: 0

    This is perfect again thank you, I have a final column which is a Unit Price column. This should apper on screen with a "minimum" number of decimal places. In my picture the 10.1234. 12.53 are perfect, however the 50, 201 and 0 should be shown as 50.00, 201.00 and 0.00.

    {
    data: "UnitPriceFC", title: "Unit Price", name: "UnitPriceFC", type: 'integer', className: "dt-head-right dt-body-right dataTable-editable-progress editable", render: function (data, type, row) {
    var nR = $.fn.dataTable.render.number(',', '.', 4).display;
    var rendered = nR(data).replace('.0000', '');
    if (rendered.slice(-2) === '00' && rendered.indexOf('.') >= 0) {
    return rendered.slice(0, -2);
    }
    return rendered;
    } }

    Is it possible to modify the rendered.slice to ignore the first 2 decimal places before removing any irrelevant 0s? i tired a few different ways but could not get it perfect.

    Thank you again.

  • colincolin Posts: 15,143Questions: 1Answers: 2,586

    You could pass the number through parseFloat(), you can specify the decimal places there.

    Colin

  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406
    Answer ✓
    { data: "UnitPriceFC", title: "Unit Price", name: "UnitPriceFC", type: 'integer', className: "dt-head-right dt-body-right dataTable-editable-progress editable",
      render: function (data, type, row) {
          var nR = $.fn.dataTable.render.number( ',', '.', 4 ).display;
          var rendered = nR(data);
          if ( rendered.slice(-2) === '00' ) {
              return rendered.slice(0, -2);
          }
          if ( rendered.slice(-1) === '0' ) {
             return rendered.slice(0, -1);
          }
          return rendered;
      }
    

    A little primitive, but it should work. (Didn't bother to use a loop here.)

    This should convert 50.0000 into 50.00
    It should leave 50.0001 unchanged. And it should change 50.0010 to 50.001

  • RyanG94RyanG94 Posts: 11Questions: 3Answers: 0

    Sorted, thank you for your time. I have learned a lot about what is possible with the rendering function here.

Sign In or Register to comment.