serhii / short-number
For changing the language you want to call `set` method once before calling other methods from this package.
Requires
- php: ^8.1
Requires (Dev)
- laravel/pint: v1.18.3
- phpstan/phpstan: ^2.0.3
- phpunit/phpunit: ^10.5.38
- squizlabs/php_codesniffer: ^3.11.1
- symfony/var-dumper: ^6.4.15
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
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 and can be easily extended with new languages.
Follow the full documentation for this package
Requirements
If you use Podman or Docker then requirements are already satisfied by the container image. Otherwise, make sure you have the following installed:
- PHP 8.1 or higher
mbstringextension (used for theupperCaseoption)
Installation
composer require serhii/short-number
Usage
use Serhii\ShortNumber\Number; Number::short(0); // '0' Number::short(999); // '999' Number::short(1234); // '1k' Number::short(5_935_235); // '5m' Number::short(-1234); // '-1k'
Numbers below 1000 are returned as is. Numbers are shortened up to 18 digits (999 quadrillion), anything larger throws Serhii\ShortNumber\Exceptions\LargeNumberException.
Languages
English is used by default. Set another language once before calling Number::short():
use Serhii\ShortNumber\Lang; use Serhii\ShortNumber\Number; Lang::set('ru'); Number::short(2000); // '2ΡΡΡ'
| Flag | Language | Code |
|---|---|---|
| π¬π§ | English | en |
| π·πΊ | Russian | ru |
| πΊπ¦ | Ukrainian | uk |
| π¨π³ | Chinese | zh |
| π―π΅ | Japanese | ja |
Lang::current() returns the active language code and Lang::resetToDefaults() restores English.
Development
Install dependencies and run the full check suite:
composer install composer check
Individual commands:
| Command | Description |
|---|---|
composer test |
Run PHPUnit tests |
composer stan |
Run PHPStan static analysis |
composer cs |
Run PHP CodeSniffer |
composer pint |
Check code style with Laravel Pint |
composer check |
Runs all the above commands at once |
OCI
If you use OCI tools like Podman or Docker, you can use podman-compose or docker compose to build and run the container. See instructions below. Note, we use podman-compose in the examples, but you can replace it with docker compose if you prefer.
Build an Image
To build an image, navigate to the root of the project and run this:
podman-compose build
Run the Container
To run a container, navigate to the root of the project and run this:
podman-compose up -d
Enter the Container
Enter a container if you need to run composer:
podman-compose exec app bash
Inside the container you can use PHP and Composer like you normally would. For example, to install dependencies, run this:
composer install
Cleanup
You can remove networks and cleanup by running this:
podman-compose down
License
The Short Number project is licensed under the MIT License