• Resolved sonof101

    (@sonof101)


    Hi, is there yet any way to abbreviate the counts, for example to display 1k rather than 1000?

    Thanks, Steve

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic 🚀

    The plugin relies on WordPress’ own number_format_i18n function to display the numbers. That function includes its own filter that you could use to customize the display of the numbers to fit your needs, by hooking into the filter from your own code in a custom snippet.

    I hope this helps.

    Thread Starter sonof101

    (@sonof101)

    Hi Jeremy, thanks for responding. I found the following code. How would I adapt it to work for this instance? Sorry if I sound stupid but I’m not a developer. Any help very much appreciated.

     /**  * Shorten long numbers to K/M/B (Thousand,  Million and Billion)  *  * @param int $number The number to shorten.  * @param int $decimals Precision of the number of decimal places.  * @param string $suffix A string displays as the number suffix.  */ if(! function_exists('short_number')) { function short_number($n, $decimals = 2, $suffix = '') {     if(!$suffix)         $suffix = 'K, M,B';     $suffix = explode(',', $suffix);     if ($n < 1000) { // any number less than a Thousand         $shorted = number_format($n);     } elseif ($n < 1000000) { // any number less than a million         $shorted = number_format($n/1000, $decimals).$ suffix[0];     } elseif ($n < 1000000000) { // any number less than a billion         $shorted = number_format($n/1000000, $decimals).$ suffix[1];     } else { // at least a billion         $shorted = number_format($n/1000000000, $decimals).$ suffix[2];     }     return $shorted; } }
    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic 🚀

    Expanding on that code snippet you found, here is how you could integrate it with WordPress, for example:

     /** * Change the display of large numbers on the site. * * @see  https://wordpress.org/support/topic/abbreviate-numbers-with-k/ * * @param string $formatted Converted number in string format. * @param float  $number     The number to convert based on locale. * @param int    $decimals   Precision of the number of decimal places. */ function jeherve_custom_large_numbers( $formatted, $number, $decimals ) { global $wp_locale; $decimals      = 0; $thousants_sep = ','; if ( isset( $wp_locale ) ) { $decimals      = (int) $wp_locale->number_format['decimal_point']; $thousands_sep = $wp_locale->number_format['thousands_sep']; } if ( $number < 1000 ) { // any number less than a Thousand. return number_format( $number, $decimals, $thousands_sep ); } else if ( $number < 1000000 ) { // any number less than a million return number_format( $number / 1000, $decimals, $thousands_sep ) . ' K'; } else if ( $number < 1000000 ) { // any number less than a billion return number_format( $number / 1000000, $decimals, $thousands_sep ) . ' M'; } else { // at least a billion return number_format( $number / 1000000000, $decimals, $thousands_sep ) . ' B'; } // Default fallback.  We should not get here. return $formatted; } add_filter( 'number_format_i18n', 'jeherve_custom_large_numbers', 10, 3 );
    Thread Starter sonof101

    (@sonof101)

    Hi Jeremy, thank you. I tried your snippet but got this back…

    syntax error, unexpected token “}”

    Thread Starter sonof101

    (@sonof101)

    I fixed it and edited it to make it display a number after the decimal on posts above 1000 views. This code below is now working well. Thank you!

    /**

    • Change the display of large numbers on the site.
      *
    • @see https://wordpress.org/support/topic/abbreviate-numbers-with-k/
      *
    • @param string $formatted Converted number in string format.
    • @param float $number The number to convert based on locale.
    • @param int $decimals Precision of the number of decimal places.
      */
      function jeherve_custom_large_numbers( $formatted, $number, $decimals ) {
      global $wp_locale; $decimals = 0; // No decimal place for numbers less than 1000
      $thousands_sep = ‘,’; if ( isset( $wp_locale ) ) {
      $thousands_sep = $wp_locale->number_format[‘thousands_sep’];
      } if ( $number < 1000 ) { // any number less than a Thousand.
      return number_format( $number, $decimals, $thousands_sep );
      } else if ( $number < 1000000 ) { // any number less than a million
      $decimals = 1; // One decimal place for numbers greater than 1000
      return ”.number_format( $number / 1000, $decimals, ‘.’, $ thousands_sep ).’K’;
      } else if ( $number < 1000000000 ) { // any number less than a billion
      $decimals = 1; // One decimal place for numbers greater than 1000
      return ”.number_format( $number / 1000000, $decimals, ‘.’, $ thousands_sep ).’M’;
      } else { // at least a billion
      $decimals = 1; // One decimal place for numbers greater than 1000
      return ”.number_format( $number / 1000000000, $decimals, ‘.’, $ thousands_sep ).’B’;
      } // Default fallback. We should not get here.
      return $formatted;
      }
      add_filter( ‘number_format_i18n’, ‘jeherve_custom_large_numbers’, 10, 3 );
    • This reply was modified 1 month ago by sonof101 .
Viewing 5 replies - 1 through 5 (of 5 total)