We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 51020
    • 670 Posts
    Hi There,
    I have a number as a chunk:
    MyNumber = 1,234,567.89
    


    Which is used throughout the site.

    One one page I need to show the full number - e.g.
    £[[$MyNumber]]
    



    But on another page I want to show round the number down so it's more readable - e.g.

    Turnover of over 1.2 million

    So I need to create a snippet which rounds the number to the nearest hundred thousand.
    £[[$MyNumber:numberFormat=`nearestHundredThousand`]]
    


    This occurs in different pages where I need to show 1,234 as 'over 1,200' so

    £[[$MyNumber:numberFormat=`nearestHundred`]]
    



    Any ideas how I can do this?
    Sorry - I'm not a PHP whizz - I'm sure this is simple to do!


    Thanks in advance!
    Andy

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

      • 51020
      • 670 Posts
      UPDATE - I have tried using the numberFormat snippet I found in docs:

      <?php
      $number = floatval($input);
      $optionsXpld = @explode('&', $options);
      $optionsArray = array();
      foreach ($optionsXpld as $xpld) {
          $params = @explode('=', $xpld);
          array_walk($params, create_function('&$v', '$v = trim($v);'));
          if (isset($params[1])) {
              $optionsArray[$params[0]] = $params[1];
          } else {
              $optionsArray[$params[0]] = '';
          }
      }
      $decimals = isset($optionsArray['decimals']) ? $optionsArray['decimals'] : null;
      $dec_point = isset($optionsArray['dec_point']) ? $optionsArray['dec_point'] : null;
      $thousands_sep = isset($optionsArray['thousands_sep']) ? $optionsArray['thousands_sep'] : null;
      $output = number_format($number, $decimals, $dec_point, $thousands_sep);
      return $output;
      
      


      Then in my page:

      [[$bigNumber:numberFormat=`&decimals=2&dec_point=.&thousands_sep=,`]]
      


      With [[$bigNumber being: 12345678.90

      But I cannot seem to get it to work - it is rounding the number, but only to the first decimal point.
      I want to display it as 1.2 (i.e. 1.2 million)

      I think I'm on the right track - but cannot seem to manipulate the results correctly!

      Andy
        • 51020
        • 670 Posts
        OK - so I got the above to work by changing the inpout number to:
        1.23456789

        And all is working - BUT - it's rounding the numbers to the nearest decimal - but I don't want it to - I want it display: 1.292333 as 1.2 - but with the above snippet, it's displaying this as 1.3

        Any ideas?
        This is the final bit of the puzzle! (proud of myself for self-solving 99% of it!!! smiley
          • 38783
          • 571 Posts
          I'm not too good with php yet, but just looking at what you are trying to achieve without discussing the code, if you Round 1.29 the correct answer would be 1.3. Perhaps rather than Rounding you want to look for something that Truncates the number?
            If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

            email: [email protected] | website: https://andytough.com
            • 38783
            • 571 Posts
            Although, just to contradict my previous comment, this page suggests it should be possible to get the snippet to Round down.

            http://php.net/manual/en/function.round.php
              If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

              email: [email protected] | website: https://andytough.com
              • 51020
              • 670 Posts
              Quote from: andytough at May 12, 2017, 07:34 AM
              I'm not too good with php yet, but just looking at what you are trying to achieve without discussing the code, if you Round 1.29 the correct answer would be 1.3. Perhaps rather than Rounding you want to look for something that Truncates the number?

              Hi there - thanks for this.
              The reason I don't want to round up is because we're talking about money - the text says 'we raised over £1.2 million - but it says 'we raised over 1.3Million - which is wrong.

              Yes- I think I am lookin to just truncate the number.
              So all I want is to turn:
              12345678.90
              into
              over 1.2

              then another example:
              123456

              to

              over 12,000

              Make sense?
              Thanks again
              Andy
                • 38783
                • 571 Posts
                That makes sense. Lets hope someone with some good php skills drops by the Forums soon!
                  If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

                  email: [email protected] | website: https://andytough.com
                  • 51020
                  • 670 Posts
                  So after playing around, the code above isn't going to work.
                  I have several numbers I want to format, such as:

                  1,234,567.89
                  1,234
                  123,456

                  Which need to be output as:
                  1.2 million
                  over 1,200
                  over 123,000

                  Does anyone know a way to do this?

                  Thanks
                  Andy
                  • discuss.answer
                    • 36816
                    • 109 Posts
                    Created a quick hack that matches your use case as I see it, but gives you a bit of flexibility for how to output numbers with different numbers of digits. It's a bit inelegant and procedural, but appears to give what you're looking for.

                    Tinker with the 5 arrays at the top to adjust the display to different input lengths. NOTE: Only good as-is for up to 999,999,999, but you could add more array entries if you want to go higher. See sample outputs below code.
                    <?php
                        // Configure the following 5 arrays to control output based on
                        // the number of digits in the input number
                    
                        // Cases where output will be displayed in multiples, i.e. "5 million"
                        // instead of "5,000,000". Enter the number to divide by (eg. 1000000 for a million,
                        // 1000 for a thousand, 1 for no division)
                        $displayDivisor = array(
                            1 => 1,
                            2 => 1,
                            3 => 1,
                            4 => 1,
                            5 => 1,
                            6 => 1,
                            7 => 1000000,
                            8 => 1000000,
                            9 => 1000000
                        );
                        
                        // Precision of rounding of input. Eg. 3 rounds at thousands
                        // 6 rounds at millions, 5 rounds to tenth of million, etc.
                        $roundingDigits = array(
                            1 => 0,
                            2 => 0,
                            3 => 0,
                            4 => 2,
                            5 => 3,
                            6 => 3,
                            7 => 5,
                            8 => 6,
                            9 => 6
                        );
                        
                        // How many decimal places to display in the output
                        $displayDecimals = array(
                            1 => 0,
                            2 => 0,
                            3 => 0,
                            4 => 0,
                            5 => 0,
                            6 => 0,
                            7 => 1,
                            8 => 0,
                            9 => 0
                        );
                    
                        // Text prefix for output
                        $displayPrefix = array(
                            1 => '',
                            2 => '',
                            3 => '',
                            4 => 'more than ',
                            5 => 'more than ',
                            6 => 'more than ',
                            7 => 'more than ',
                            8 => 'more than ',
                            9 => 'more than '
                        );
                        
                        // Text suffix for output
                        $displaySuffix = array(
                            1 => '',
                            2 => '',
                            3 => '',
                            4 => '',
                            5 => '',
                            6 => '',
                            7 => ' million',
                            8 => ' million',
                            9 => ' million'
                        );
                    
                    
                        // CHANGE NOTHING AFTER THIS POINT
                        $value              = floatval($input);
                        $digitCount         = intval(log10($value)) + 1;
                        
                        if (! isset($displayDivisor[$digitCount]) || 
                            ! isset($roundingDigits[$digitCount]) || 
                            ! isset($displayDecimals[$digitCount]) || 
                            ! isset($displayPrefix[$digitCount]) || 
                            ! isset($displaySuffix[$digitCount])
                            ) { return ''; }
                        
                        $roundingPrecision  = pow(10, $roundingDigits[$digitCount]);
                        $floorValue         = floor($value / $roundingPrecision) * $roundingPrecision / $displayDivisor[$digitCount];
                        $outputValue        = number_format($floorValue, $displayDecimals[$digitCount]);
                        
                        $output = $displayPrefix[$digitCount] .  
                                  $outputValue .
                                  $displaySuffix[$digitCount];
                    
                        return $output;
                    


                    Sample (Input | Output)

                    • 1 | 1
                    • 12 | 12
                    • 123 | 123
                    • 1234 | more than 1,200
                    • 12345 | more than 12,000
                    • 123456 | more than 123,000
                    • 1234567 | more than 1.2 million
                    • 12345678 | more than 12 million
                    • 123456789 | more than 123 million
                    [ed. note: clareoconsulting last edited this post 9 years, 4 months ago.]
                      • 4385
                      • 372 Posts
                      Output an easy-to-read number (U.S. english)

                      $[[++Assets:fmt.humanNumber]]


                      Outputs:
                      247,704,360 -> 247.7 million
                      866,965,260,000 -> 867 billion

                      or

                      [[++Financial_Benefits:fmt.humanNumber=`stripWords=1&decimals=1`]]


                      Outputs:
                      247,704,360 -> 247.7
                      866,965,260,000 -> 867

                      <?php
                      $optionsXpld = @explode('&', $options);
                      $optionsArray = array();
                      foreach ($optionsXpld as $xpld) {
                          $params = @explode('=', $xpld);
                          array_walk($params, create_function('&$v', '$v = trim($v);'));
                          if (isset($params[1])) {
                              $optionsArray[$params[0]] = $params[1];
                          } else {
                              $optionsArray[$params[0]] = '';
                          }
                      }
                      $stripWords = isset($optionsArray['stripWords']) ? $optionsArray['stripWords'] : null;
                      $decimals = isset($optionsArray['decimals']) ? $optionsArray['decimals'] : 0;
                      #    Output easy-to-read numbers
                      #    by james at bandit.co.nz
                      $number = $input;
                              // first strip any formatting;
                              $n = (0+str_replace(",","",$number));
                              
                              // is this a number?
                              if(!is_numeric($n)) return false;
                              
                              // now filter it;
                          if ($n>1000000000000):
                          $num = round(($n/1000000000000),1);
                          $wrd = 'trillion';
                      elseif ($n>1000000000):
                          $num = round(($n/1000000000),1);
                          $wrd = 'billion';
                      elseif ($n>1000000):
                          $num = round(($n/1000000),1);
                          $wrd = 'million';
                      elseif ($n>1000):
                          $num = round(($n/1000),1);
                          $wrd = 'thousand';
                      elseif ($n>100):
                          $num = round(($n/100),1);
                          $wrd = 'hundred';
                      endif;
                      if($stripWords=1){
                              $output = number_format($num, $decimals);
                      }else{
                              $output = number_format($num, $decimals) . " " . $wrd;    
                      }
                              return $output;
                      
                        DropboxUploader -- Upload files to a Dropbox account.
                        DIG -- Dynamic Image Generator
                        gus -- Google URL Shortener
                        makeQR -- Uses google chart api to make QR codes.
                        MODxTweeter -- Update your twitter status on publish.