Documentation

Lightweight package shortens given number to a short representation of it. For example 1234 will be formatted to 1k and 20244023 to 20m. Package supports multiple languages, the default it's set to English.

🐘 Supported PHP versions

  • βœ… 7.2
  • βœ… 7.3
  • βœ… 7.4
  • βœ… 8.0
  • βœ… 8.1
  • βœ… 8.2
  • βœ… 8.3

βš™οΈ Language configurations

Change language

For changing the language you want to call set() method once before calling other methods from this package.

Serhii\ShortNumber\Lang::set('ru');

Overwrite translations

If you want to replace existing translations for supported language or add your own language, you can pass custom translations as the second argument to set() method.

// Overwriting all fields
Serhii\ShortNumber\Lang::set('en', [
    'thousand' => 'thou',
    'million' => 'mil',
    'billion' => 'bil',
    'trillion' => 'tril',
]);

You can overwrite any fields that you need, overwriting all fields is not necessary.

// Overwriting 1 field
Serhii\ShortNumber\Lang::set('en', ['million' => 'mil']);

🚩 Supported languages

FlagLanguageCode (ISO 639-1)ThousandMillionBillionTrillion
πŸ‡¬πŸ‡§Englishen1k1m1b1t
πŸ‡·πŸ‡ΊRussianru1тыс1ΠΌΠ»Π½1ΠΌΠ»Π΄1Ρ‚Ρ€Π½
πŸ‡ΊπŸ‡¦Ukrainianuk1тис1ΠΌΠ»Π½1ΠΌΠ»Π΄1Ρ‚Ρ€Π½

πŸ‘ Usage

use Serhii\ShortNumber\Number;

Number::conv(1893234); // returns: 1m
Number::conv(20234); // returns: 20m

🎁 Contribute language support

Here is the commitopen in new window that added support for Ukrainian language. Contribute another language is very simple. You need to make 3 steps:

Step 1. Translations

Add translations to resources/translations.php file. Here is the file:

return [
    // English
    'en' => [
        'thousand' => 'k',
        'million' => 'm',
        'billion' => 'b',
        'trillion' => 't',
    ],
    // Russian
    'ru' => [
        'thousand' => 'тыс',
        'million' => 'ΠΌΠ»Π½',
        'billion' => 'ΠΌΠ»Π΄',
        'trillion' => 'Ρ‚Ρ€Π½',
    ],
    // add same for your language
];

Step 2. Tests

After adding another language, you need to add line to tests/TranslationsTest.php. The method runTestsForSuffixes(string $lang, string[] $suffixes) will generate tests for you. You just need to run ./vendor/bin/phpunit to make sure your translation works.

class TranslationsTest extends TestCase
{
    public function testing_correct_conversion(): void
    {
        $this->runTestsForSuffixes('en', ['k', 'm', 'b', 't']);
        $this->runTestsForSuffixes('ru', ['тыс', 'ΠΌΠ»Π½', 'ΠΌΠ»Π΄', 'Ρ‚Ρ€Π½']);
        // add same line for your language here
    }
}

Step 3. README.md

Last step you need to add new language to README.md file under the section Supported languages.

πŸš€ Quick start

composer require serhii/short-number
Last Updated:
Contributors: SerhiiCho