2017 © Pedro Peláez
 

library money-math

gmp-based arbitrary precision operations on currency amounts "XXX.YY"; because floats are BAD for representing money

image

azathoth/money-math

gmp-based arbitrary precision operations on currency amounts "XXX.YY"; because floats are BAD for representing money

  • Sunday, November 16, 2014
  • by azathoth
  • Repository
  • 2 Watchers
  • 1 Stars
  • 10 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 8 Forks
  • 0 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

Build Status, (*1)

What does it do?

Arithmetic operations on currency amounts. Amounts on input and output are arbitrary large and precise:, (*2)

99999999999999999999999999999999999999999999999999999999999999999999999999999999.99
+
0.01
=
100000000000000000000000000000000000000000000000000000000000000000000000000000000.00

However, in cases when the division is involved — like for percentage calculation — the result is rounded to the whole cent: 33% of $0.50 is $0.17 instead of $0.165, (*3)

As a bonus feature, there's a simple formatting function for amounts in CHF, EUR, USD, GBP, and JPY., (*4)

Why does it exist?

Because storing currency amounts in floats is a really bad idea, (*5)

How to use it?

Installation

Install via Composer package manager:, (*6)

Just create a composer.json file for your project:, (*7)

{
    "require": {
        "ikr/money-math": "0.1.*"
    }
}

And run these two commands to install the Composer dependencies:, (*8)

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

Now you can add the Composer's autoloader, and you will have access to the MoneyMath\* classes:, (*9)

<?php
require 'vendor/autoload.php';

API

use MoneyMath\Decimal2;
use MoneyMath\Currency;

$a = new Decimal2("3.50");
$b = new Decimal2("4.50");

$a->integerValue()                          // 3
$a->fractionValue()                         // 50
$a->centsValue()                            // 350

strval(Decimal2::plus($a, $b))              // "8.00"
strval(Decimal2::sum([$a, $a, $b]))         // "11.50"
strval(Decimal2::avg([$a, $b]))             // "4.00"
strval(Decimal2::minus($a, $b))             // "-1.00"
strval(Decimal2::staticMultiply($a, 2))           // "7.00"
strval(Decimal2::mul($a, $b))               // "15.75"

strval(Decimal2::div($a, $b))               // "0.78"
strval(Decimal2::getPercentsOf($a, $b))     // "0.16" b% of a
strval(Decimal2::cmp($a, $b))               // -1

And last, but not least :), (*10)

$c = new Decimal2("42.02");
strval(Decimal2::roundUpTo5Cents($c))       // "42.05"

Which we use for bills in CHF that are required by law to be 0 (mod 5)., (*11)

For formatting please use the Currency class, (*12)

(new Currency('EUR'))->format(new Decimal2('-100000000000.00')) // -100.000.000.000,00

License (MIT)

Copyright (c) 2014 Ivan Krechetov, (*13)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:, (*14)

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software., (*15)

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE., (*16)

The Versions

16/11 2014

dev-master

9999999-dev https://github.com/ikr/money-math-php

gmp-based arbitrary precision operations on currency amounts "XXX.YY"; because floats are BAD for representing money

  Sources   Download

MIT

The Requires

 

The Development Requires

by Matěj Račinský
by Ivan Krechetov

currency money format arithmetic gmp bigint bignum arbitrary precision

16/11 2014

0.1.4

0.1.4.0 https://github.com/ikr/money-math-php

gmp-based arbitrary precision operations on currency amounts "XXX.YY"; because floats are BAD for representing money

  Sources   Download

MIT

The Requires

 

The Development Requires

by Matěj Račinský
by Ivan Krechetov

currency money format arithmetic gmp bigint bignum arbitrary precision

16/11 2014

0.1.3

0.1.3.0 https://github.com/ikr/money-math-php

gmp-based arbitrary precision operations on currency amounts "XXX.YY"; because floats are BAD for representing money

  Sources   Download

MIT

The Requires

 

The Development Requires

by Matěj Račinský
by Ivan Krechetov

currency money format arithmetic gmp bigint bignum arbitrary precision

04/09 2013

v0.1.2

0.1.2.0 https://github.com/ikr/money-math-php

gmp-based arbitrary precision operations on currency amounts "XXX.YY"; because floats are BAD for representing money

  Sources   Download

MIT

The Requires

  • php >=5.4.0
  • ext-gmp *

 

The Development Requires

by Ivan Krechetov

currency money format arithmetic gmp bigint bignum arbitrary precision

04/09 2013

v0.1.1

0.1.1.0 https://github.com/ikr/money-math-php

gmp-based arbitrary precision operations on currency amounts "XXX.YY"; because floats are BAD for representing money

  Sources   Download

MIT

The Requires

  • php >=5.3.3
  • ext-gmp *

 

The Development Requires

by Ivan Krechetov

currency money format arithmetic gmp bigint bignum arbitrary precision

21/09 2012

0.1.0

0.1.0.0 https://github.com/ikr/money-math-php

gmp-based arbitrary precision operations on currency amounts "XXX.YY"; because floats are BAD for representing money

  Sources   Download

The Requires

  • php >=5.3.3
  • ext-gmp *

 

The Development Requires

by Ivan Krechetov

currency money format arithmetic gmp bigint bignum arbitrary precision