kiendaotac/laravel-number-to-words

Laravel 数字转越南文汉字

2.0.1 2021-04-30 18:35 UTC

This package is auto-updated.

Last update: 2024-09-27 15:50:33 UTC


README

Laravel Number To Words


Latest version Build status Quantity score StyleCI Total download License

信息

Laravel 数字转越南文汉字

安装

通过 Composer 安装 Laravel Number To Words

composer require phpviet/laravel-number-to-words

用法

扩展功能

数字转汉字

  • 通过 facade 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); 

数字转货币

  • 通过 facade 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\DictionaryDictionary 类,或者实现 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 来贡献代码。谢谢!