Skip to content

Configure Output ​

You can apply additional configurations to control how numbers are formatted across your application. For example, if you want to display numbers with a specific number of decimal places, you can set this configuration in your provider or bootstrap file.

Configuration Example ​

Below is an example of how to configure decimal formatting:

php
use Serhii\ShortNumber\Number;
use Serhii\ShortNumber\Config;

Number::configure(new Config(decimals: 1, upperCase: true));

Once configured, all numbers will be formatted with the specified number of decimal places. For instance:

php
use Serhii\ShortNumber\Number;

echo Number::short(12_345); // output: "12.3K"

Lock the Configuration ​

Available starting from version 4.5.0. In long-running workers, such as Octane, RoadRunner, Swoole or FrankenPHP in worker mode, the configuration survives between requests. Use configureOnce() to set it once and prevent further changes:

php
use Serhii\ShortNumber\Config;
use Serhii\ShortNumber\Number;

Number::configureOnce(new Config(decimals: 1));

echo Number::short(5_935_235); // output: "5.9m"

Number::configure(new Config(decimals: 2)); // throws LogicException

configure() can be called multiple times until configureOnce() locks it. Changing the language with Lang::set() is still allowed.

Available Configurations ​

Here is the full list of all available configurations for the latest version:

OptionTypeDefaultDescription
decimals`intnull`null
decimalsSeparatorstring.Separator for the decimals
suffixSeparatorstring''Separator between the number and its suffix. An empty string by default
upperCaseboolfalseWhether to show the suffix in uppercase

The decimals option truncates the digits instead of rounding them.

Decimals vs Significant Digits ​

Some language sets define significantDigits on their FormulaRule objects to control how many digits are kept by default. If you explicitly pass decimals to Config, it takes priority over significantDigits.

For example, the Chinese set keeps 2 significant digits by default:

php
use Serhii\ShortNumber\Config;
use Serhii\ShortNumber\Lang;
use Serhii\ShortNumber\Number;

Lang::set('zh');

echo Number::short(12_345); // output: "1.2万"

Number::configure(new Config(decimals: 3));
echo Number::short(12_345); // output: "1.234万"

Number::configure(new Config(decimals: 0));
echo Number::short(12_345); // output: "1万"

When decimals is not provided, the significantDigits of the matched rule is used. When the rule has no significantDigits, the number is formatted with 0 decimals by default.