Format Numbers

Format Numbers

dobulet302dobulet302 Posts: 38Questions: 0Answers: 0
edited August 2010 in General
I have a table with decimal places and comma's and I am not using php to format my numbers as when I do it causes a sorting issue. Is there a plugin that will format all my numbers? Example 1000 will be 1,000 and 1233.323522 will be 1,233.323522.

Replies

  • ruzzruzz Posts: 49Questions: 0Answers: 0
    You could always try google?

    Here's one: http://javascript.about.com/library/blnumfmt.htm
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    There is code in DataTables which will do this for you actually: http://datatables.net/usage/callbacks#fnFormatNumber . See this thread: http://datatables.net/forums/comments.php?DiscussionID=2274 . Or you can just rip the code out of DataTables and use it as you wish.

    Allan
  • dobulet302dobulet302 Posts: 38Questions: 0Answers: 0
    not sure if I follow, so like this?

    [code]

    $('#reports_table').dataTable( {
    "sDom": '<"H"lTfr>t<"F"ip>',
    "bPaginate": false,
    "bJQueryUI": true,
    "fnFormatNumber": function (iIn) {
    if ( iIn < 1000 ) {
    return iIn;
    } else {
    var s=(iIn+""), a=s.split(""), out="", iLen=s.length;
    for ( var i=0 ; i



    } );
    [/code]

    sorry javascript isn't really my strong language, and I thank you for your assistance I will be making a donation.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    You need to use fnRender ( http://datatables.net/usage/columns#fnRender ) and probably http://datatables.net/usage/columns#bUseRendered as false (to maintain your sorting), in combination with fnFormatNumber.

    So for example:

    [code]
    $('#example').dataTable( {
    "aoColumnDefs": [
    {
    "aTargets": [ 0 ],
    "bUseRendered": false,
    "fnRender": function ( o ) {
    return o.oSettings.fnFormatNumber( parseFloat( o.aData[ o.iDataColumn ] ) );
    }
    }
    ]
    } );
    [/code]
    Basically what this does is to target the first column (aTargets:[0]) with a rendering function which will take the data for that column and format it using the internal DataTables function...

    How does that go for you?

    Allan
This discussion has been closed.