cable8mm / n-format
Small NumberFormatter Extension Library
Requires
- php: ^8.0
Requires (Dev)
- laravel/pint: ^1.0
- phpunit/phpunit: ^9.0|^10.0|^11.0
Suggests
- laravel/pint: Provides fixing all codes for coding style
This package is auto-updated.
Last update: 2025-02-25 09:27:47 UTC
README
PHP already includes NumberFormat classes and functions, but they may not be available for some countries like Korea and Japan. Therefore, we provide a small wrapper library to extend NumberFormat, similar to how Carbon extends DateTime. Additionally, some additional functions have been provided.
If you have used Laravel, you could use NFormatHelper
helper class. Refer to the Usage Laravel Helper section.
We have provided the API Documentation on the web. For more information, please visit https://www.palgle.com/n-format/ ❤️
Install
composer require cable8mm/n-format
Usage
General:
print NFormat::currency(358762); // default locale = 'ko_KR' currency = 'KRW' //=> ₩358,762
print NFormat::spellOut(5); // default locale = 'ko_KR' currency = 'KRW' //=> 오
NFormat::$locale = 'ja_JP'; print NFormat::spellOut(5); //=> 五
print NFormat::decimal(12346); //=> 12,346 print NFormat::percent(12346); //=> 1,234,600% print NFormat::rawPercent(12346); //=> 12,346%
New special method ordinalSpellOut
and currencySpellOut
(only ko_KR):
print NFormat::ordinalSpellOut(10); //=> 열번째 print NFormat::currencySpellOut(12346); //=> 12,346 원
You can also use price()
and smartPrice()
to calculate the price for customers.
print NFormat::price(12346, -2); //=> 12300 print NFormat::price(12346.23, 1); //=> 12346.20 print NFormat::smartPrice(12346); //=> 12300 print NFormat::smartPrice(123467); //=> 123000 print NFormat::smartPrice(1234678); //=> 1230000 print NFormat::smartPrice(12346432); //=> 12350000 print NFormat::smartPrice(3212343232); //=> 3212340000
Laravel Helper
You can utilize this in Laravel Blade without any need for installation:
{{ NFormatHelper::currency(12346) }}
Formatting
composer lint # Modify all files to comply with the PSR-12. composer inspect # Inspect all files to ensure compliance with PSR-12.
Test
composer test
License
The N-Format is open-sourced software licensed under the MIT license.