apoyan/hack-money

处理货币和货币转换的 Hack 库。

安装: 15

依赖: 0

建议者: 0

安全: 0

星标: 4

关注者: 3

分支: 2

开放问题: 0

语言:Hack

dev-master / 1.0.x-dev 2017-03-21 20:41 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:31:05 UTC


README

处理货币和货币转换的 Hack 库

Latest Stable Version Total Downloads Latest Unstable Version License

安装

Hack Money 通过 Composer 安装。要安装,只需将其添加到您的 composer.json 文件中

{
    "require": {
        "apoyan/hack-money": "~1.0"
    }
}

然后运行 composer 更新您的依赖项

$ curl -s http://getcomposer.org/installer | php
$ php composer.phar update

用法

<?php
use Money\Money as Money;

# Money
$currency = new Currency('USD');

$money = new Money(10.00, $currency);
$money_2 = new Money('10.00', $currency);
$money = new Money(10, "EUR");
$money = new Money(10.00, "USD");

$money->getAmount(); // => 10.00
$money->getCurrency(); // => "USD"

# Comparisons
$money->equals($money_2); // => false
$money->equals($money); // => true
$money->isSameCurrency($money_2); // => true

# Arithmetic
$money->add($money_2)->getAmount(); // => "110.00"
$money->sub(new Money(10, "USD"))->getAmount(); // => "10.00"
$money->div(new Money(10, "EUR"))->getAmount(); // => "Exception: Currencies must be identical"
$money->mul(new Money(2, "USD"))->getAmount(); // => "20.00"

# Formatting

$currency = new Currency('USD');
$money = new Money("1000000.00", $currency);

$money->format(
  [
    "with_currency" => true,
    "no_cents_if_whole" => true,
    "thousands_separator" => ' ',
    "decimal_mark" => ',',
    "with_symbol" => true,
    "symbol_position" => "before"
  ]
); // => "$1 000 000 USD"

货币

<?php
$currency = new Currency('USD');
$currency_2 = new Currency('EUR');
$currency_3 = new Currency('USD');

$currency->getCode(); // => "USD"
$currency->getName(); // => "United States Dollar"
$currency->getSymbol(); // => "$"
$currency->exponent(); // => 2

$currency->equals($currency_2); // => false
$currency->equals($currency_3); // => true
$currency->equals("USD"); // => true