Giter Site home page Giter Site logo

superbalist / php-money Goto Github PK

View Code? Open in Web Editor NEW
11.0 40.0 1.0 100 KB

A money and currency library for handling arbitrary-precision arithmetic

License: MIT License

PHP 100.00%
php-money currency-library money arbitrary-precision php superbalist bcmath

php-money's Introduction

php-money

A money and currency library for handling arbitrary-precision arithmetic

Author Build Status StyleCI Software License Packagist Version Total Downloads

Installation

composer require superbalist/php-money

Usage

use Superbalist\Money\CurrencyConversionServiceProvider;
use Superbalist\Money\CurrencyFactory;
use Superbalist\Money\Money;
use Superbalist\Money\OpenExchangeRatesCurrencyConversionService;

// set default currency conversion service
// this is optional, and is only required if you're going to be converting between currencies
$service = new OpenExchangeRatesCurrencyConversionService('[[insert app id here]]');
CurrencyConversionServiceProvider::setCurrencyConversionService($service);

// set default currency
CurrencyFactory::setDefault('ZAR');

// a money object can be constructed very loosely
$a = new Money(150);
$a = new Money('150'); // recommended
$a = new Money('150.00');
$a = new Money(150.00); // not recommended, as we're trying to avoid using floating points
$a = new Money('150', CurrencyFactory::make('USD'));

// basic operations on numbers
$a = new Money('150.55');
$b = new Money('99.45');
$c = $a->add($b); // 250

$a = new Money('300.00');
$b = new Money('2');
$c = $a->divide($b); // 150

// comparing numbers
$a = new Money('0.33');
$b = new Money('0.33');
$c = $a->equals($b); // true
$c = $a->isGreaterThan($b); // false
$c = $a->isGreaterThanOrEqualTo($b); // true
$c = $a->isLessThan($b); // false

// convert a monetary value from currency A to currency B
$a = new Money('100', CurrencyFactory::make('ZAR'));
$b = $a->toCurrency(CurrencyFactory::make('USD'));

// format a number for display
$a = new Money('123.9999');
echo $a->format(); // 123.99
echo $a->display(); // R123.99

// handling value added tax
$a = new Money('100');
$rate = '0.14';
$b = $a->calculateVat($rate); // 14
$c = $a->add($b); // 114
$d = $c->calculateNetVatAmount($rate); // 100

// min & max
$a = new Money('100');
$b = new Money('101');
$c = $a->max($b); // 101
$d = $a->min($b); // 100

// ...see classes for full list of methods

php-money's People

Contributors

matthewgoslett avatar nicja avatar shad0wfir3 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

isabella232

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.