convert to money format

convert to money format

ricardomuller90ricardomuller90 Posts: 1Questions: 1Answers: 0

Hi, how do i to convert 'pageTotal' variable to money format ? for example, the result is showing 10.5, instead of 10.50

$(document).ready(function() {
$('#example').DataTable( {

    "footerCallback": function ( row, data, start, end, display ) {
        var api = this.api(), data;

        // Remove the formatting to get integer data for summation
        var intVal = function ( i ) {
            return typeof i === 'string' ?
                i.replace(/[\$,]/g, '')*1 :
                typeof i === 'number' ?
                    i : 0;
        };

        // Total over all pages
        total = api
            .column( 9 )
            .data()
            .reduce( function (a, b) {
                return intVal(a) + intVal(b);
            }, 0 );

        // Total over this page
        pageTotal = api
            .column( 9, { page: 'current'} )
            .data()
            .reduce( function (a, b) {
                return intVal(a) + intVal(b);
            }, 0 );


        // Update footer
        $( api.column( 9 ).footer() ).html(
            'R$ '+pageTotal +' não pago'
        );
    }


} );

} );

Answers

  • allanallan Posts: 63,463Questions: 1Answers: 10,466 Site admin

    Easiest there is simply pageTotal.toFixed(2). But if you want more flexibility, such as thousands separators, you can use DataTables' built in number renderer if you want:

    var display = $.fn.dataTable.render.number( ',', '.', 0, 'R$' ).display;
    var formattedNumber = display( pageTotal );
    

    Allan

This discussion has been closed.