descubraomundo / money
表示货币价值(使用货币的最小单位)的值对象
v1.6.3
2016-05-09 17:16 UTC
Requires
- php: ^5.4|^7.0
- ext-intl: *
README
表示使用货币的最小单位表示的值对象,具体是使用货币的最小单位表示的货币价值。
这是一个从sebastianbergmann/money分支出来的版本,我们实现了自己的 Packagist,因为原始项目已经被放弃。
安装
如果您使用 Composer 管理项目的依赖,只需在项目的 composer.json
文件中添加对 descubraomundo/money
的依赖即可。
以下是一个仅定义对 Money 依赖的 composer.json
文件的示例
{
"require": {
"descubraomundo/money": "^1.6"
}
}
使用示例
创建一个 Money 对象并访问其货币值
use SebastianBergmann\Money\Currency; use SebastianBergmann\Money\Money; // Create Money object that represents 1 EUR $m = new Money(100, new Currency('EUR')); // Access the Money object's monetary value print $m->getAmount(); // Access the Money object's monetary value converted to its base units print $m->getConvertedAmount();
上面的代码产生以下输出
100
1.00
从字符串值创建一个 Money 对象
use SebastianBergmann\Money\Currency; use SebastianBergmann\Money\Money; // Create Money object that represents 12.34 EUR $m = Money::fromString('12.34', new Currency('EUR')) // Access the Money object's monetary value print $m->getAmount();
上面的代码产生以下输出
1234
使用特定于货币的 Money 子类
use SebastianBergmann\Money\EUR; // Create Money object that represents 1 EUR $m = new EUR(100); // Access the Money object's monetary value print $m->getAmount();
上面的代码产生以下输出
100
请注意,没有特定于土耳其里拉(TRY)的 Money
子类,因为 TRY
在 PHP 中不是一个有效的类名。
使用 PHP 内置的 NumberFormatter 格式化 Money 对象
use SebastianBergmann\Money\Currency; use SebastianBergmann\Money\Money; use SebastianBergmann\Money\IntlFormatter; // Create Money object that represents 1 EUR $m = new Money(100, new Currency('EUR')); // Format a Money object using PHP's built-in NumberFormatter (German locale) $f = new IntlFormatter('de_DE'); print $f->format($m);
上面的代码产生以下输出
1,00 €
使用 Money 对象进行基本算术运算
use SebastianBergmann\Money\Currency; use SebastianBergmann\Money\Money; // Create two Money objects that represent 1 EUR and 2 EUR, respectively $a = new Money(100, new Currency('EUR')); $b = new Money(200, new Currency('EUR')); // Negate a Money object $c = $a->negate(); print $c->getAmount(); // Calculate the sum of two Money objects $c = $a->add($b); print $c->getAmount(); // Calculate the difference of two Money objects $c = $b->subtract($a); print $c->getAmount(); // Multiply a Money object with a factor $c = $a->multiply(2); print $c->getAmount();
上面的代码产生以下输出
-100
300
100
200
比较 Money 对象
use SebastianBergmann\Money\Currency; use SebastianBergmann\Money\Money; // Create two Money objects that represent 1 EUR and 2 EUR, respectively $a = new Money(100, new Currency('EUR')); $b = new Money(200, new Currency('EUR')); var_dump($a->lessThan($b)); var_dump($a->greaterThan($b)); var_dump($b->lessThan($a)); var_dump($b->greaterThan($a)); var_dump($a->compareTo($b)); var_dump($a->compareTo($a)); var_dump($b->compareTo($a));
上面的代码产生以下输出
bool(true)
bool(false)
bool(false)
bool(true)
int(-1)
int(0)
int(1)
compareTo()
方法返回一个整数,小于、等于或大于零,如果认为一个 Money
对象的值分别小于、等于或大于另一个 Money
对象。
您可以使用 compareTo()
方法结合 PHP 内置的排序函数对 Money
对象数组进行排序
use SebastianBergmann\Money\Currency; use SebastianBergmann\Money\Money; $m = array( new Money(300, new Currency('EUR')), new Money(100, new Currency('EUR')), new Money(200, new Currency('EUR')) ); usort( $m, function ($a, $b) { return $a->compareTo($b); } ); foreach ($m as $_m) { print $_m->getAmount() . "\n"; }
上面的代码产生以下输出
100
200
300
将表示为 Money 对象的货币值分配给 N 个目标
use SebastianBergmann\Money\Currency; use SebastianBergmann\Money\Money; // Create a Money object that represents 0,99 EUR $a = new Money(99, new Currency('EUR')); foreach ($a->allocateToTargets(10) as $t) { print $t->getAmount() . "\n"; }
上面的代码产生以下输出
10
10
10
10
10
10
10
10
10
9
使用比率列表将表示为 Money 对象的货币值进行分配
use SebastianBergmann\Money\Currency; use SebastianBergmann\Money\Money; // Create a Money object that represents 0,05 EUR $a = new Money(5, new Currency('EUR')); foreach ($a->allocateByRatios(array(3, 7)) as $t) { print $t->getAmount() . "\n"; }
上面的代码产生以下输出
2
3
从表示为 Money 对象的货币值中提取百分比(以及小计)
use SebastianBergmann\Money\Currency; use SebastianBergmann\Money\Money; // Create a Money object that represents 100,00 EUR $original = new Money(10000, new Currency('EUR')); // Extract 21% (and the corresponding subtotal) $extract = $original->extractPercentage(21); printf( "%d = %d + %d\n", $original->getAmount(), $extract['subtotal']->getAmount(), $extract['percentage']->getAmount() );
上面的代码产生以下输出
10000 = 8265 + 1735
请注意,这会从已包含百分比的货币值中提取百分比。如果您想获取货币值的百分比,应使用乘法(例如,使用 multiply(0.21)
计算表示为 Money 对象的货币值的 21%)。