2017 © Pedro Peláez
 

library money

Simple money manipulation library

image

krak/money

Simple money manipulation library

  • Monday, June 26, 2017
  • by ragboyjr
  • Repository
  • 1 Watchers
  • 0 Stars
  • 389 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

Money

Simple money manipulation library which uses bcmath to manipulate money values appropriately., (*1)

Install

Install via composer at krak/money, (*2)

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., (*3)

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., (*4)

f($money)

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

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., (*6)

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., (*7)

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., (*8)

Tests

make test

The Versions

26/06 2017

dev-master

9999999-dev

Simple money manipulation library

  Sources   Download

The Requires

  • php >=5.6

 

The Development Requires

26/06 2017

v0.2.2

0.2.2.0

Simple money manipulation library

  Sources   Download

The Requires

  • php >=5.6

 

The Development Requires

14/02 2017

v0.2.1

0.2.1.0

Simple money manipulation library

  Sources   Download

The Requires

  • php >=5.6

 

The Development Requires

28/11 2016

v0.2.0

0.2.0.0

Simple money manipulation library

  Sources   Download

The Requires

  • php >=5.6

 

The Development Requires

16/04 2016

v0.1.0

0.1.0.0

Simple money manipulation library

  Sources   Download

The Requires

 

The Development Requires