Currency formatting

Currency formatting

jose.gomezjose.gomez Posts: 4Questions: 2Answers: 0

Hello,

I've been struggling a little bit with European currency formatting and ordering the columns.

Right now I have the following:

Table init

var data_columns = [
            { 'data': 'seleccionado'},
            ....More columns here...
            {'data': 'van', "type": "num-fmt"},
            {'data': 'inversion_neta', "type": "num-fmt"},
         ];

    window.table = $('#tramosTable').DataTable( {
        "data":           tableData,
        "dataSrc":        "",
        "deferRender":    true,
        "columns":        data_columns,
        "pageLength":     25,
        "stateSave":      true,
        "autoWidth":      true,
        "language": {
                "url": 'url_to_the_file"
            },
    } );

Language file:

{
    "decimal":        ",",
    "thousands":      ".",
    "emptyTable":     "No hay datos disponibles para esta tabla. Es posible que aún no se hayan cargado o que haya habido un error en el servidor. Por favor, prueba de clicar de nuevo en el link que abre la tabla después de unos segundos.",
    "info":           "Mostrando _START_ a _END_ de _TOTAL_ entradas",
    "infoEmpty":      "Mostrando 0 a 0 de 0 entradas",
    "infoFiltered":   "filtrado de un total de _MAX_ registros",
    "infoPostFix":    "",
    "lengthMenu":     "Mostrar _MENU_ registros",
    "loadingRecords": "Cargando...",
    "processing":     "Procesando...",
    "search":         "Busqueda:",
    "zeroRecords":    "No se ha encontrado ningún registo igual",
    "paginate": {
        "first":      "Primero",
        "last":       "Último",
        "next":       "Siguiente",
        "previous":   "Anterior"
    },
    "aria": {
        "sortAscending":  ": Activar para ordenar ascedentemente",
        "sortDescending": ": Activar para ordenar descendentemente"
    }
}

The datatable strings are being changed for the translation, but the columns with the num-fmt types are not being formatted correctly using the

 "decimal":        ",",
 "thousands":      ".",

In fact are being formatted the predefined way.

My question is: Am I doing something wrong? Why the data is not being correctly formatted?

Before doing this I was formatting the money amounts manually with a render function and using the numeral.js library:

'render': function ( data, type, full, meta ){return numeral(data).format('0.0000');}

But, the ordering was totally incorrect as the punctuation in Europe is different from the american that is using datatables by default.

Can anyone help me with this? If you need more information, I'll try to attach it.

Thank you very much.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,439Questions: 1Answers: 10,052 Site admin
    Answer ✓

    Could you give me a link to the page so I can debug it directly please. if that isn't possible, can you give show me the data that tableData contains.

    If the data contains numbers that are number types (rather than a string) then I would suggest you use the built in number renderer which will do all the formatting for you and also will correctly handle sorting.

    Allan

  • jose.gomezjose.gomez Posts: 4Questions: 2Answers: 0
    edited June 2016

    Allan,

    Thank you very much for your solution. Now it's working perfectly using

    {
        data: 'price',
        render: $.fn.dataTable.render.number( ',', '.', 2 )
    }
    

    This way of formatting makes more sense than using an external library and it's more compact.

    Thank you for your quick answer.

This discussion has been closed.