Share

A function to make large numbers easier to read or take up less space

Sometimes it’s useful to make large numbers more ‘human’ or smaller so they fit in a table. With this function, you can reduce numbers to thousands and millions or K and M.

You can also define how many decimal places you want and also theres an extra function to show you how to add a shortcode if you should need it.

Examples: 32345678 -> 32.35 million, 5678 -> 5.68K.

The Code

Usage

You’ll notice there are 2 functions. The second one is only necessary if you intend to use this function as a shortcode. It also shows how a function with parameters can be easily be used as a shortcode too.

Within php code:

fphp_human_number ( <NUMBER>, <DECIMAL PLACES>, <HUMAN or METRIC> )

e.g. <?php if (function_exists(‘fphp_human_number’)) echo fphp_human_number(‘123456’, ‘2’, ‘human’); ?>

As a shortcode:

[humannumber num=<NUMBER> places=<DECIMAL PLACES> type=<HUMAN or METRIC>]

e.g. [humannumber num=123456 places=2 type=human]

The result with the parameters in the example would be 123.46 thousand.

Parameters

  • <NUMBER> = The number you want to convert.
  • <DECIMAL PLACES> = Decimal places in result. Default is 1
  • <HUMAN or METRIC> = ‘human’ returns numbers in thousands and millions, ‘metric’ returns numbers in K and M. Default is human

Share