2017 © Pedro Pelรกez
 

library money-type

A PHP library for a precise money handling data type. Avoids floating point rounding errors.

image

phpexpertsinc/money-type

A PHP library for a precise money handling data type. Avoids floating point rounding errors.

  • Monday, May 1, 2017
  • by hopeseekr
  • Repository
  • 1 Watchers
  • 0 Stars
  • 4,638 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 4 Versions
  • 9 % Grown

The README.md

Money Type

TravisCI Maintainability Test Coverage, (*1)

MoneyType is a PHP Experts, Inc., Project meant to keep track of money precisely, even up to millions of dollars., (*2)

It is never safe to store money as floats or even integers times one hundred. This project gives you the security you need, so that something like the bug in "Office Space" just won't happen to your enterprise application., (*3)

Installation

Via Composer, (*4)

composer require phpexperts/money

Usage

use PHPExperts\MoneyType\Money;

$money = new Money(5.22);

$money->add(0.55);
echo "$money\n"; // 5.77

# It keeps precision much much better than mere cents.
$money->subtract(0.0001);
echo "$money\n"; // 5.77
$money->subtract(0.004);
echo "$money\n"; // 5.77
$money->subtract(0.001);
echo "$money\n"; // 5.76

# Actually, to the tenth decimal place.
# So it's Cryptocurrency-ready!
$money->multiply(55.7773);
echo "$money\n"; // 321.55

# But deep down, it stores the true values to many decimals (if you use BCMath).
echo $money->getWithFullPrecision() . "\n"; // 321.5508738395

$money->divide('1.000005');
echo "$money\n"; // 321.55
echo $money->getWithFullPrecision() . "\n"; // 321.5492660931

# You can also compare really large numbers with one another, up to 10 decimal places.
# PHP Experts' MoneyType is cryptocurrency ready! In fact, that's what it was designed for!
$money->compare(321.5492660931);       // 0 = equal
$money->compare(321.549266093009);     // -1 = less
$money->compare(321.5492660931000001); // 1 = more

# Get the object.
print_r($money);

# It is cryptocurrency ready:
# Converts Bitcoins to Satoshis
$btc = '1.55527331';
$satoshis = NumberHelper::convertToCents($btc, 8); // 155527331 (int)

Use cases

PHPExperts\MoneyType\Money
โœ” Can only be instantiated with a numeric string
โœ” Will report what strategy is being used
โœ” Will use BCMath if it is available
โœ” Will fall back to native php if necessary
โœ” Proxies everything to its calculation strategy
โœ” Confirm that the readme demo works, (*5)

PHPExperts\MoneyType\Internal\BCMathCalcStrategy
โœ” Can get the full precision value to more than sixteen decimals
โœ” Can add with high precision
โœ” Can subtract with high precision
โœ” Can multiply with high precision
โœ” Can divide with high precision
โœ” Can compare two numbers with high precision
โœ” Can compute high precision modulus
โœ” Can compute the modulus of decimals
โœ” Can round with high precision
โœ” Can only be instantiated with a numeric string
โœ” Access the object as a string to get its valuation
โœ” Can add with cent precision
โœ” Can subtract with cent precision
โœ” Can multiply with cent precision
โœ” Can divide with cent precision
โœ” Can compare two numbers with cent precision
โœ” Will not accept a non number for any operation, (*6)

PHPExperts\MoneyType\Internal\NativeCalcStrategy
โœ” Wont attempt operations with non numbers
โœ” Can get the full precision value to two decimals
โœ” Cannot compute the modulus of decimals
โœ” Will throw an exception if asked to compute an integer modulus
โœ” Can compute the modulus of integers if dev passes i am a dummy parameter
โœ” Can only be instantiated with a numeric string
โœ” Access the object as a string to get its valuation
โœ” Can add with cent precision
โœ” Can subtract with cent precision
โœ” Can multiply with cent precision
โœ” Can divide with cent precision
โœ” Can compare two numbers with cent precision
โœ” Will not accept a non number for any operation, (*7)

PHPExperts\MoneyType\Internal\NumberHelper: A collection of functions for number manipulation.
โœ” Will return true if given a float
โœ” Will return true if given a float string
โœ” Will return false if given an integer
โœ” Will return false if given an integer string
โœ” Will throw an exception if given anything else
โœ” Can convert dollars to cents integer without precision loss
โœ” Can convert bitcoins to satoshis, (*8)

Testing

phpunit

Contributors

Theodore R. Smith theodore@phpexperts.pro
GPG Fingerprint: 4BF8 2613 1C34 87AC D28F 2AD8 EB24 A91D D612 5690
CEO: PHP Experts, Inc., (*9)

[Alix Axel]
who contributed the base version of our "bcround()" function from https://stackoverflow.com/a/1653826/430062., (*10)

License

MIT license. Please see the license file for more information., (*11)

The Versions

01/05 2017

dev-master

9999999-dev

A PHP library for a precise money handling data type. Avoids floating point rounding errors.

  Sources   Download

MIT

The Development Requires

by Theodore R. Smith

php currency money

01/05 2017

v1.0.2

1.0.2.0

A PHP library for a precise money handling data type. Avoids floating point rounding errors.

  Sources   Download

MIT

The Development Requires

by Theodore R. Smith

php currency money

01/05 2017

v1.0.1

1.0.1.0

A PHP library for a precise money handling data type. Avoids floating point rounding errors.

  Sources   Download

MIT

The Development Requires

by Theodore R. Smith

php currency money

01/05 2017

v1.0.0

1.0.0.0

A PHP library for a precise money handling data type. Avoids floating point rounding errors.

  Sources   Download

MIT

The Development Requires

by Theodore R. Smith

php currency money