We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 16899
    • 284 Posts
    Hi guys

    How can a format a numeric string into a number, using an custom output filter? I want for ex. 1900000 into 1,900,000€ when applied to a TV in this way
    [[*example:custom-output-filter]]
    .

    Is this possible? My idea is detect from thousand to > 10 millions and add a separator for units.

    Ex.
    1000 to 1,000
    10000 to 10,000
    100000 to 100,000
    1000000 to 1,000,000
    10000000 to 10,000,000

    Many thanks in advance and sorry about my english

    This question has been answered by legues. See the first response.

      • 28042 ☆ A M B ☆
      • 24,524 Posts
      This is usually done by setting the "locale" in PHP.
      http://php.net/manual/en/function.setlocale.php

      Or you can create a snippet and use the PHP NumberFormatter class (PHP >= 5.3)
      http://www.php.net/manual/en/class.numberformatter.php
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 32699 ☆ A M B ☆
        • 427 Posts
        If you are going to use the Snippet as a custom filter per your example, please make sure the filter and snippet names are identical and the snippet returns the value.
          Get your copy of MODX Revolution Building the Web Your Way http://www.sanitypress.com/books/modx-revolution-building-the-web-your-way.html

          Check out my MODX || xPDO resources here: http://www.shawnwilkerson.com
          • 16899
          • 284 Posts
          Hi

          First of all many thanks for the replies!!! Really usefull!! I´m not a coder, so my question is how can i get a custom filter based in these php code?

          <?php 
          $a = new \NumberFormatter("it-IT", \NumberFormatter::CURRENCY); 
          echo $a->format(12345.12345) . "<br>"; // outputs €12.345,12 
          ?>
          


          I tried to put this code into a snippet called test and then i´ve applied this to a TV by doing
          [[*tvname:test]]
          but it´s not working... Gave me an internal server error!!! Why? sad

          Many thanks in advance!!
          • discuss.answer
            • 16899
            • 284 Posts
            Quote from: legues at Jun 07, 2013, 03:54 AM
            Hi

            First of all many thanks for the replies!!! Really usefull!! I´m not a coder, so my question is how can i get a custom filter based in these php code?

            <!--?php 
            $a = new \NumberFormatter("it-IT", \NumberFormatter::CURRENCY); 
            echo $a--->format(12345.12345) . "
            "; // outputs €12.345,12 
            ?>
            


            I tried to put this code into a snippet called test and then i´ve applied this to a TV by doing
            [[*tvname:test]]
            but it´s not working... Gave me an internal server error!!! Why? sad

            Many thanks in advance!!

            Hi again

            I´ve found my solution in Modx documentation and it´s working perfectly - smiley
            http://rtfm.modx.com/display/revolution20/Custom+Output+Filter+Examples#CustomOutputFilterExamples-numberformat

            Many thanks to both to try to help me!! smiley