PHP code example of uniondrug / money

1. Go to this page and download the library: Download uniondrug/money library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

uniondrug / money example snippets



/**
 * test.php
 *
 */
Uniondrug\Money\Money;

$money = Money::create(5000); // 构造一个货币对象
echo $money;
echo "\n";


$money = $money->add(Money::create(600)); // 与另一个货币对象相加
echo $money;
echo "\n";

$money = $money->subtract(Money::create(500)); // 与另一个货币对象相减
echo $money;
echo "\n";

$money = $money->multiply(4); // 乘以
echo $money;
echo "\n";

$money = $money->divide(3); // 除以
echo $money;
echo "\n";

// 判断
$money->isZero();
$money->isNegative();
$money->isPositive();
$money->equals(Money::create(800));
$money->lessThan(Money::create(800));
$money->greaterThan(Money::create(800));
$money->lessThanOrEqual(Money::create(800));

// 格式化
echo $money->format(2, '.', '');
echo "\n";

$ php test.php
CNY 5,000.00
CNY 5,600.00
CNY 5,100.00
CNY 20,400.00
CNY 6,800.00
CNY 2,266.67
2266.67