<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
    <link href="https://nightly.datatables.net/css/jquery.dataTables.css" rel="stylesheet" type="text/css" />
    <script src="https://nightly.datatables.net/js/jquery.dataTables.js"></script>
    <script src="https://cdn.datatables.net/plug-ins/1.13.4/dataRender/percentageBars.js"></script>
    <meta charset=utf-8 />
    <title>DataTables - JS Bin</title>
  </head>
  <body>
    <div class="container">
      <table id="example" class="display nowrap" width="100%">
        <thead>
          <tr>
            <th>Name</th>
            <th>Value 1</th>
            <th>Value 2</th>
            <th>Percentage</th>
          </tr>
        </thead>
        <tfoot>
          <tr>
            <th>Name</th>
            <th>Value 1</th>
            <th>Value 2</th>
            <th>Percentage</th>
          </tr>
        </tfoot>
        <tbody>
          <tr>
            <td>Tiger Nixon</td>
            <td>123.123.123,99</td>
            <td>3.333.333,88</td>
            <td></td>
          </tr>
          <tr>
            <td>Garrett Winters</td>
            <td>10</td>
            <td>5</td>
            <td></td>
          </tr>
        </tbody>
      </table>
    </div>
  </body>
</html>
 
body {
  font: 90%/1.45em "Helvetica Neue", HelveticaNeue, Verdana, Arial, Helvetica, sans-serif;
  margin: 0;
  padding: 0;
  color: #333;
  background-color: #fff;
}
 
var lang = 'de'; //decimal comma and period as thousand separator
// var lang = 'en'; //decimal point and comma as thousand separator
var floatVal = function (i) {
    if (lang == 'de') {
       return typeof i === 'string' ? i.replace(/[\.]/g, '').replace(/[\,]/g, '.') * 1 : typeof i === 'number' ? i : 0;
     } else {
       return typeof i === 'string' ? i.replace(/[\,]/g, '') * 1 : typeof i === 'number' ? i : 0;
     } 
};
if (lang == 'de') {
  var numberRenderer = $.fn.dataTable.render.number( '.', ',', 2 ).display;
} else {
  var numberRenderer = $.fn.dataTable.render.number( ',', '.', 2 ).display;
}
var table = new DataTable('#example', {
  columnDefs: [
    {
      targets: 3,
      render: function (data, type, row, meta) {
        // Remove the formatting to get integer data for summation
        return type === 'display'
        ? numberRenderer(100 * floatVal(row[2]) / floatVal(row[1]) ) + '%'
        : data;
      },
    }
  ]
});
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers