voskobovich/yii2-price-formatter

为Yii2转换价格的组件

1.1.2 2016-05-10 19:13 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:47:39 UTC


README

此组件可以将货币值(价格)在定点表示(整数作为分)和浮点表示(浮点数作为美元和分)之间转换。

License Latest Stable Version Latest Unstable Version Total Downloads

支持

GutHub问题.

查看示例

美元的示例。

将3.99美元转换为399分

Yii::$app->get('priceFormatter')->toStore(3.99); // input float
Yii::$app->get('priceFormatter')->toStore('3,99'); // input string
// Result: 399

将3美元99美分转换为399分

Yii::$app->get('priceFormatter')->toStoreByParts(3, 99);
// Result: 399

将399分转换为3.99美元

Yii::$app->get('priceFormatter')->toEdit(399);
// Result: 3.99

将399分转换为3美元,99美分

Yii::$app->get('priceFormatter')->toEditByParts(399);
// Result array:
[
    0 => 3,
    1 => 99
]

将399分转换为$3.99(带有货币符号)

Yii::$app->get('priceFormatter')->toView(399);
// Result: $3,99

安装

安装此扩展的首选方式是通过composer

运行以下命令

php composer.phar require --prefer-dist voskobovich/yii2-price-formatter "~1.0"

或添加

"voskobovich/yii2-price-formatter": "~1.0"

到您的composer.json文件的require部分。

使用方法

在您的应用配置文件中配置组件

[
    ...
    'components' => [
        'priceFormatter' => [
            'class' => 'voskobovich\price\components\PriceFormatter',
//            'currencyCode' => 'USD',
            'currencyCode' => function($component) {
                return Yii::$app->user->identity->currency_code;
            }
        ]
    ]
]
``