krak/money

There is no license information available for the latest version (v0.2.2) of this package.

Simple money manipulation library

v0.2.2 2017-06-26 09:15 UTC

This package is auto-updated.

Last update: 2024-12-18 17:35:43 UTC


README

Simple money manipulation library which uses bcmath to manipulate money values appropriately.

Install

Install via composer at krak/money

Usage

<?php

use Krak\Money;

$calc = Money\calc($precision = 2);
$res = $calc->add('1.00', '2.00');
$res = $calc->mul($res, 2);

API

calc($precision = 2)

Returns a cached instance of the BCMathCalculator. Set the precision to higher if you need to do any multiplications or divisions with the money that might require extra precision.

preciseCalc()

Returns a cached instance of the FloatCalculator. Use this calculator if you have to do any intense money calculations like calculate compounding interest where you need a LOT of precision. Once done, you should then use the money\f to format the resulting money as proper money.

f($money)

Format the money by rounding it to two decimal places and returning a properly formatted money string of \d+\.\d{2}

interface Calculator

<?php

interface Calculator {
    public function add($a, $b);
    public function sub($a, $b);
    public function mul($a, $b);
    public function div($a, $b);
    public function cmp($a, $b);
}

These methods are fairly self explanatory, the cmp method will return 0 if $a and $b are equal, > 0 if $a is > $b and < 0 else.

abstract class AbstractCalculator

<?php

abstract class AbstractCalculator implements Calculator {
    public function sum(...$args);
    public function diff(...$args);
    public function quot(...$args);
    public function prod(...$args);
    /** returns the max value of the set */
    public function max(...$args);
    /** returns the min value of the set */
    public function min(...$args);
    /** returns true if $a < $b */
    public function lt($a, $b);
    /** returns true if $a <= $b */
    public function lte($a, $b);
    /** returns true if $a > $b */
    public function gt($a, $b);
    /** returns true if $a >= $b */
    public function gte($a, $b);
    /** returns true if $a == $b */
    public function eq($a, $b);
    /** returns true if $a != $b */
    public function neq($a, $b);

    abstract public function add($a, $b);
    abstract public function sub($a, $b);
    abstract public function mul($a, $b);
    abstract public function div($a, $b);
    abstract public function cmp($a, $b);
}

Any calculator should extend this class instead of directly implementing the Calculator interface so that it can have these extra methods.

Each method simply will find the sum, difference, quotient, product, max, or min of the set of args. They delegate the actual calculations to the abstract functions.

Tests

make test