Created
September 21, 2018 00:59
-
-
Save afmicc/25a621405c4a78673042e73b14370941 to your computer and use it in GitHub Desktop.
Render function to numeric column type for datatables.net - Parameters: "," decimal separator and "." thousand separator.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// usage | |
$("#dataTable").DataTable( | |
{ | |
"columns": [ | |
{ | |
"data": "precio", | |
"name": "Precio", | |
"render": $.fn.dataTable.render.customNumberFormat('.', ',', 6) | |
} | |
] | |
}); | |
// define format | |
$.fn.dataTable.render.customNumberFormat = function (thousands, decimal, precision, prefix, postfix) | |
{ | |
return function(d) | |
{ | |
if (typeof d !== 'number' && typeof d !== 'string') | |
{ | |
return d; | |
} | |
var negative = d < 0 ? '-' : ''; | |
var flo = parseFloat(d); | |
// If NaN then there isn't much formatting that we can do - just | |
// return immediately, escaping any HTML (this was supposed to | |
// be a number after all) | |
if (isNaN(flo)) | |
{ | |
return __htmlEscapeEntities(d); | |
} | |
flo = flo.toFixed(precision); | |
d = Math.abs(flo); | |
var intPart = parseInt(d, 10); | |
var floatPart = precision ? | |
decimal + Number((d - intPart).toFixed(precision)).toString().substring(2) : | |
''; | |
return negative + (prefix || '') + | |
intPart.toString().replace( | |
/\B(?=(\d{3})+(?!\d))/g, thousands | |
) + | |
floatPart + | |
(postfix || ''); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment