ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

How to format the output of a text field as a number?

June 12, 2012 10:08am

Subscribe [2]
  • #1 / Jun 12, 2012 10:08am

    Iceberg Creative

    21 posts

    Hello.

    I have a ‘Text Input’ field in one of my channels. It has been assigned a ‘Field Content’ value of ‘Number’. How can I get the output of this field to display as a number with commas in the correct places; tens, thousands etc.?

    Cheers.

  • #2 / Jun 14, 2012 12:23pm

    boldyellow

    89 posts

    One approach would be to use the PHP function number_format (but you’d need to change your template prefs to allow PHP).

    Another option is put that function into a little homemade plug-in, which then lets you leave PHP off in your template:

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Number
    {
        public $return_data = "";
    
        public function __construct()
        {
            $this->EE =& get_instance();
            $this->return_data = number_format($this->EE->TMPL->tagdata);
        }
    }
    
    /* End of file pi.number.php */
    /* Location: ./system/expressionengine/third_party/number/pi.number.php */

    You’d call it in your template like…

    {exp:number}
    
       {my_number}    {!-- say your number is: 8822090993330000 --)
    
    {/exp:number}

    ... and get 8,822,090,993,330,000 output on your page.

     

  • #3 / Jun 14, 2012 6:04pm

    Iceberg Creative

    21 posts

    Hi boldyellow

    Thanks for taking the time to respond with such a technical answer. I really do appreciate it.

    I’ve only just started dabbling with EE, so the thought of building a ‘plugin’ seems a bit ominous at the moment. I’ll do some research on how to do such a thing, though.

    Thanks again.

  • #4 / Jun 14, 2012 6:15pm

    boldyellow

    89 posts

    Not to sound glib, but if I can do it, well… you know. The EE Plug-in docs are very easy to follow with examples. 

    BTW, that sample code does work, I tested it and you can just use it like that.

  • #5 / Jun 14, 2012 6:19pm

    Iceberg Creative

    21 posts

    Ha. I’ll take a look in the morning I think and let you know how I get on. Nearly midnight here so I’d better call it a day!

    It does seem as though a little function like this would be native to EE. But what do I know?

    Thanks again 😊

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases