Format a number

Format a number

hawkmasterhawkmaster Posts: 56Questions: 20Answers: 0

Hello,
I have a datatable (jquery.dataTables.min.js 1.9.4). There is a Ajax request from a DB. One column "salary" receives a number like
500000.
I would like to format the number as a currency format like:
500.000,00

I have found a JS function which can format the number but I have no idea how to build into the datatable?

Esspecially , how can you build in the function and give the parameter?

{ "mData": "salary",
"sWidth": 50,
"sType": 'number_format', // is this correct? but how can you pass the parameters?
},


function number_format (number, decimals, dec_point, thousands_sep)
{
var exponent = "";
var numberstr = number.toString ();
var eindex = numberstr.indexOf ("e");
if (eindex > -1)
{
exponent = numberstr.substring (eindex);
number = parseFloat (numberstr.substring (0, eindex));
}

if (decimals != null)
{
var temp = Math.pow (10, decimals);
number = Math.round (number * temp) / temp;
}
var sign = number < 0 ? "-" : "";
var integer = (number > 0 ?
Math.floor (number) : Math.abs (Math.ceil (number))).toString ();

var fractional = number.toString ().substring (integer.length + sign.length);
dec_point = dec_point != null ? dec_point : ".";
fractional = decimals != null && decimals > 0 || fractional.length > 1 ?
(dec_point + fractional.substring (1)) : "";
if (decimals != null && decimals > 0)
{
for (i = fractional.length - 1, z = decimals; i < z; ++i)
fractional += "0";
}

thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ?
thousands_sep : null;
if (thousands_sep != null && thousands_sep != "")
{
for (i = integer.length - 3; i > 0; i -= 3)
integer = integer.substring (0 , i) + thousands_sep + integer.substring (i);
}

return sign + integer + fractional + exponent;
}

Answers

  • hawkmasterhawkmaster Posts: 56Questions: 20Answers: 0

    Hello
    can nobody help me or give me an idea what to do?

    thanks a lot

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    I have found a JS function which can format the number but I have no idea how to build into the data table?

    Use the mRender option ( http://legacy.datatables.net/ref#mRender ). When the data type requested is display then format the number and return it. Otherwise just return the raw number.

    Allan

This discussion has been closed.