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.

Thousand Separator (Number Formatting)

July 18, 2012 1:21pm

Subscribe [2]
  • #1 / Jul 18, 2012 1:21pm

    Evolve Websites

    110 posts

    Hi there,

    I’m presenting some prices on a website and (much like the post featured here: http://ellislab.com/forums/viewthread/216773/) would like to add a thousand separator where relevant.

    I came across a plugin (http://devot-ee.com/add-ons/number-format), but the EE2 version leads to a 404 on Github.

    I’m sure this must be a simple thing to do.

    Any ideas are appreciated?

  • #2 / Jul 28, 2012 5:01pm

    Here’s a script I’ve used with great success:

    // JavaScript Document
    
    function trimNumber(s) {
      while (s.substr(0,1) == '0' && s.length>1) { s = s.substr(1,9999); }
       s += '';
     x = s.split('.');
     x1 = x[0];
     x2 = x.length > 1 ? '.' + x[1] : '';
     var rgx = /(\d+)(\d{3})/;
     while (rgx.test(x1)) {
      x1 = x1.replace(rgx, '$1' + ',' + '$2');
     }
     return x1 + x2;
    }

    Place that script in the head of your page, and call to it where you want a number to be formatted:

    <s-cript language="JavaScript" type="text/javascript">
    <!--
    var s = '{your_custom_field_name}';
    d-ocument.write(trimNumber(s));
     -->
    </s-cript>

    you’ll have to take out the “-” in s-cript and d-ocument. I had to add that, so I could get the code to post in the forum.

    Good luck.

     

  • #3 / Jul 30, 2012 3:52am

    Evolve Websites

    110 posts

    Hi there,

    Thanks for your response, I’ve now sorted this one using the Price Format plugin (http://devot-ee.com/add-ons/price-format), but thanks for your suggestion also.

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

ExpressionEngine News!

#eecms, #events, #releases