leshobster / money
PHP money_format() 的简单且跨平台的替代方案。支持 91 种货币,包括 INR。
Requires
- php: >=5.5.9
Requires (Dev)
- phpunit/phpunit: 5.4.*
README
Money 是一个小的 PHP 库,帮助你将数字格式化为货币,包括 INR(印度卢比)。它是对 money_format() 的简单替代。
示例
<?php $money = new Gerardojbaez\Money\Money(12.99, 'USD') $money->format(); // RESULT: $12.99 // You can also use the included helper: moneyFormat(12.99, 'USD'); // RESULT: $12.99
内容
安装
通过 Composer(文件 composer.json
)拉取此包
{ "require": { "gerardojbaez/money": "0.*" } }
然后运行
composer update
<?php require 'vendor/autoload.php'; use Gerardojbaez\Money\Money; $money = new Money(100); $money->format();
基本用法
支持的货币
ARS, AMD, AWG, AUD, BSD, BHD, BDT, BZD, BMD, BOB, BAM, BWP, BRL, BND, CAD, KYD, CLP, CNY, COP, CRC, HRK, CUC, CUP, CYP, CZK, DKK, DOP, XCD, EGP, SVC, EUR, GHC, GIP, GTQ, HNL, HKD, HUF, ISK, INR, IDR, IRR, JMD, JPY, JOD, KES, KWD, LVL, LBP, LTL, MKD, MYR, MTL, MUR, MXN, MZM, NPR, ANG, ILS, TRY, NZD, NOK, PKR, PEN, UYU, PHP, PLN, GBP, OMR, RON, ROL, RUB, SAR, SGD, SKK, SIT, ZAR, KRW, SZL, SEK, CHF, TZS, THB, TOP, AED, UAH, USD, VUV, VEF, VEB, VND, ZWD。
使用辅助函数进行格式化
<?php // USD echo moneyFormat(100); // RESULT: $100.00 echo moneyFormat(10000); // RESULT: $10,000.00 // INR echo moneyFormat(100, 'INR'); // RESULT: र100 echo moneyFormat(1000000, 'INR'); // RESULT: र10,00,000
使用类进行格式化
<?php $money = new Gerardojbaez\Money\Money(1000000, 'INR'); echo $money->format(); // RESULT: र10,00,000 echo $money; // The same as using $money->format() echo $money->amount(); // RESULT: 10,00,000
自定义货币格式
要使用自定义格式,创建一个具有所需货币的 Currency
类的实例,并使用设置器(见下面的示例)应用所需的格式。使用此实例(或辅助函数)与 Money
类(或辅助函数)一起最终格式化数字。
例如
$currency = new Gerardojbaez\Money\Currency('USD'); $currency->setPrecision(3); $currency->setThousandSeparator('.'); $currency->setDecimalSeparator(','); $currency->setSymbolPlacement('after'); $money = new Gerardojbaez\Money\Money(1200.9, $currency); echo $money; // RESULT: 1.200,900$ (example) // OR echo moneyFormat(1200.9, $currency);
解析字符串
<?php echo Money::parse('$1,200.90', 'USD')->toDecimal(); // RESULT: 1200.9
获取货币信息
$currency = new Gerardojbaez\Money\Currency('USD'); echo $currency->getTitle(); // US Dollar echo $currency->getCode(); // USD echo $currency->getSymbol(); // $ echo $currency->getSymbolPlacement(); // after (before|after amount) echo $currency->getPrecision(); // 2 (number of decimals) echo $currency->getThousandSeparator(); // , echo $currency->getDecimalSeparator(); // .
获取所有支持的货币
$currencies = Gerardojbaez\Money\Currency::getAllCurrencies(); // Result Example: [ 'ARS' => [ 'code' => 'ARS' 'title' => 'Argentine Peso' 'symbol' => null 'precision' => 2 'thousandSeparator' => ',' 'decimalSeparator' => '.' 'symbolPlacement' => 'before' ] 'AMD' => [ 'code' => 'AMD' 'title' => 'Armenian Dram' 'symbol' => null 'precision' => 2 'thousandSeparator' => '.' 'decimalSeparator' => ',' 'symbolPlacement' => 'before' ] ...
许可证
本软件包为免费软件,根据 MIT 许可证条款分发。