Why are numbers not formatted?

Why are numbers not formatted?

izumovizumov Posts: 178Questions: 14Answers: 0

my test case is http://montaj.vianor-konakovo.ru/goods.html.
in column 6 numbers are displayed. in the table initiation file good1003. I prescribed
```
$('#goods').DataTable( {
"language": {

"thousands": " "

},
```
and hoped that the numbers would be formatted - thousands separated by a space. But this did not happen. Why?

Replies

  • rf1234rf1234 Posts: 2,941Questions: 87Answers: 415
    edited November 2019

    This is only about the table information, not the table itself:
    https://datatables.net/reference/option/language.thousands

    To test it i used this: "thousands": " ",

    As you can see the number "10860" in the table information is now written as
    "10 860". So it works. But as you can also see the number 2172 (number of pages) is not affected by this! So there is a bug in this rather limited functionality as well.

    You have to do the formatting of your numbers in your data table yourself, I am afraid.. I do this all server side, but of course you can also do it client side. Up to you.

  • rf1234rf1234 Posts: 2,941Questions: 87Answers: 415
    edited November 2019

    Here is an example for client side rendering from my own coding. I have German and English(UK) number formatting. German: thousand separator is period and decimal point is comma. English: Opposite.

    var numberRenderer;
    if (lang == 'de') {
        numberRenderer = $.fn.dataTable.render.number( '.', ',', 2 ).display;
    } else {
        numberRenderer = $.fn.dataTable.render.number( ',', '.', 2 ).display;
    }
    
    var yourFormattedNumber = numberRenderer(yourUnformattedNumber);
    

    and this is a getFormatter for numbers on the server side:

    // custom formatters for Data Tables
    
    function getFormatterAmount($val, $lang = null, $returnFormattedZero = false) {
        if ( ( $val == 0 ) && ( ! $returnFormattedZero ) ) {
            return '';
        }
        if ( $val <= '' && ( ! is_numeric($val) ) ) {
            return '';
        };
        //if language doesn't get passed and we are in batch: we use German format
        if ( is_null($lang) ) {
            if ( isset($_SESSION['lang']) ) {
                $lang = $_SESSION['lang'];
            } else {
                $lang = 'de';
            }
        }
        if ($lang === 'de') {     
            return number_format($val, 2, ',', '.');
        } else {
            return number_format($val, 2);
        }
    }
    
This discussion has been closed.