stonec0der/shorten-nums

Shorten some numbers to this notation 1000 => 1K.

1.0.3 2020-02-03 10:45 UTC

This package is auto-updated.

Last update: 2024-11-21 01:49:17 UTC


README

Latest Version on Packagist Build Status Quality Score Total Downloads License: MIT

This is simple package to convert 12894090 views to 12.9M views. Feel free to use.

Installation

You can clone this repo

git clone https://github.com/stoneC0der/laravel-shorten-nums.git

Or via composer:

composer require stonec0der/shorten-nums

Publish the configuration file with

php artisant vendor:publish --provider="Stonec0der\ShortenNums\ShortenNumsServiceProvider"

This will publish a config file shorten-nums.php in the config folder. Set your default precision

Usage

Let's say you have a big integer value being return like 12894090 (views) and you want to display it like this 12.8M (views).

use Stonec0der\ShortenNumsFacade

...
$value = '12894090';
// Shorten
$formated_number = ShortenNumsFacade::readableNumber($value);

// Output will
// 12.9M.

If you do not need the config file you can directly pass the precision when calling any method, else the default will be use

/*
Default
This enable you to return for 1240 => 1.2K with default precisionn
and 1.24 with $precision set to 2 and so on.
*/
$value = '1240';
$precision = 2;

$formated_number = ShortenNumsFacade::readableNumber($value, $precision);
// Output
// 1.24K

If you expect the value to be between 999,999 & 999,999,999. You can directly call a method associated with Millions

$value = '8525000';
$formated_number = ShortenNumsFacade::readableMillion($value, 2);
// Output
// 8.53M

In most case you will want to use ShortenNumsFacade::readableNumer($number);. If a value less than 999 is passed the value is return as it, if it greater than 999 Trillions return // 999+T

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email stonec0dersoft@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.