yiiviet / yii2-n2w
此包已被废弃且不再维护。未建议替代包。
支持将数字转换为汉字。
1.0.0
2018-05-30 04:35 UTC
Requires
- php: >=5.6
- yiisoft/yii2: ~2.0.0
This package is auto-updated.
Last update: 2020-01-22 20:37:05 UTC
README
Yii2 扩展,支持您将数字转换为汉字。
将数字转换为汉字是构建打印发票或相关报告、申报功能不可或缺的一部分。因此,此扩展被构建以提供最简单、最可重用的数字到汉字转换功能。
安装
如果您对 composer
是新概念,请点击 此处 了解。
composer require "yiiviet/yii2-n2w"
或者在 composer.json
文件的 require
部分中添加:
"yiiviet/yii2-n2w": "*"
使用方法
此扩展是一个 behavior
,支持所有 yii2
的 components
(模型、活动记录等)。要使用它,您必须在 component
的 behaviors
方法中声明它。
示例
/** * @property int $amount */ class Order extends ActiveRecord { public function behaviors() { return [ 'n2w' => [ 'class' => 'yiiviet\n2w\Behavior', 'property' => 'amount', 'unit' => 'đồng' ] ]; } } $order = Order::findOne(1); print $order->amount; // 100000 print $order->amountFormat; // Một trăm ngàn đồng
如您所见,在声明属性 amount
后,您可以直接使用属性 amountFormat
来将数字转换为汉字,无需额外声明。
同时声明多个属性
/** * @property int $amount * @property int $tax */ class Order extends ActiveRecord { public function behaviors() { return [ 'n2w' => [ 'class' => 'yiiviet\n2w\Behavior', 'properties' => ['amount', 'tax'], 'unit' => 'đồng' ] ]; } } $order = Order::findOne(1); print $order->amount; // 100000 print $order->amountFormat; // Một trăm ngàn đồng print $order->tax; // 10000 print $order->taxFormat; // Mười ngàn đồng
如果您想将 suffix
修改为其他字符串,则声明如下:
/** * @property int $amount * @property int $tax */ class Order extends ActiveRecord { public function behaviors() { return [ 'n2w' => [ 'class' => 'yiiviet\n2w\Behavior', 'properties' => ['amount', 'tax'], 'unit' => 'đồng', 'suffix' => 'Convert' ] ]; } } $order = Order::findOne(1); print $order->amount; // 100000 print $order->amountConvert; // Một trăm ngàn đồng print $order->tax; // 10000 print $order->taxConvert; // Mười ngàn đồng