phpviet / laravel-number-to-words
Laravel 数值转文字,支持将数字转换为越南文数字。
2.0.1
2021-04-30 18:35 UTC
Requires
- illuminate/support: ^6.0 || ^7.0 || ^8.0
- phpviet/number-to-words: ^1.2
Requires (Dev)
- orchestra/testbench: ^4.0 || ^5.0 || ^6.0
This package is auto-updated.
Last update: 2024-08-29 05:00:42 UTC
README
Laravel Number To Words
信息
Laravel 数值转文字,支持将数字转换为越南文数字。
安装
通过Composer安装Laravel Number To Words
composer require phpviet/laravel-number-to-words
使用方法
扩展功能
数字转数字
- 通过
N2W
外观使用
use N2W; // âm năm N2W::toWords(-5); // năm N2W::toWords(5); // năm phẩy năm N2W::toWords(5.5);
- 通过辅助函数
n2w
使用
// mười lăm n2w(15); // một trăm linh năm n2w(105); // hai mươi tư n2w(24);
数字转货币
- 通过
N2W
外观使用
use N2W; // năm triệu sáu trăm chín mươi nghìn bảy trăm đồng N2W::toCurrency(5690700);
- 通过辅助函数
n2c
使用
// chín mươi lăm triệu năm trăm nghìn hai trăm đồng n2c(95500200);
此外,您还可以通过toCurrency
方法第二个参数和n2c
函数使用其他货币单位,其中第一个元素是整数的单位,其次是分数的单位
use N2W; // sáu nghìn bảy trăm bốn mươi hai đô bảy xen N2W::toCurrency(6742.7, ['đô', 'xen']); // chín nghìn bốn trăm chín mươi hai đô mười lăm xen n2c(9492.15, ['đô', 'xen']);
改变数字读法
如果您觉得上面的读法已经足够,则可以跳过此步骤。
首先,要改变数字的读法,您需要通过以下命令发布配置文件
php artisan vendor:publish --provider="PHPViet\Laravel\NumberToWords\ServiceProvider" --tag="config"
发布完成后,我们将得到以下config/n2w.php
配置文件
return [ /** * Cấu hình từ điển mặc định theo chuẩn chung của cả nước */ 'defaults' => [ 'dictionary' => 'standard', ], 'dictionaries' => [ /** * Cấu hình các từ điển custom theo ý bạn. */ 'standard' => PHPViet\NumberToWords\Dictionary::class, 'south' => PHPViet\NumberToWords\SouthDictionary::class ] ];
现在,请将默认的standard
改为south
,所有的数字转文字和货币转换方法都将按照南方的风格读取
use N2W; // một trăm linh một => một trăm lẻ một N2W::toWords(101); // một nghìn => một ngàn N2W::toWords(1000); // hai mươi tư => hai mươi bốn N2W::toWords(24); // một trăm hai mươi tư nghìn không trăm linh một đồng => một trăm hai mươi bốn ngàn không trăm lẻ một đồng N2W::toCurrency(124001);
或者,如果您想更灵活地使用,可以指定字典
// một trăm hai mươi tư nghìn không trăm linh một n2w(124001); // một trăm hai mươi bốn ngàn không trăm lẻ một n2w(124001, 'south');
如果您想根据您的意愿更改读法,则可以创建一个继承自PHPViet\NumberToWords\Dictionary
的Dictionary
类,或者实现PHPViet\NumberToWords\DictionaryInterface
抽象类
use PHPViet\NumberToWords\Dictionary; use PHPViet\NumberToWords\Transformer; class MyDictionary extends Dictionary { /** * @inheritDoc */ public function specialTripletUnitFive(): string { return 'nhăm'; } }
然后将其声明到配置中
return [ /** * Cấu hình từ điển mặc định theo chuẩn chung của cả nước */ 'defaults' => [ 'dictionary' => 'my', ], 'dictionaries' => [ /** * Cấu hình các từ điển custom theo ý bạn. */ 'standard' => PHPViet\NumberToWords\Dictionary::class, 'south' => PHPViet\NumberToWords\SouthDictionary::class, 'my' => MyDictionary::class ] ];
然后尝试一下
use N2W; // mười nhăm N2W::toWords(15);
针对开发者
如果您觉得扩展功能不足或存在错误,并希望共同开发,我们非常欢迎!请创建issue
来贡献您的想法,或者创建PR
来贡献代码。谢谢!