Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

Decimal

Andreu Correa Casablanca edited this page Mar 24, 2015 · 10 revisions

The Decimal class has been created to make easier handling large numbers inside financial applications without precision loss.

Import

To import the Decimal class, you must add the following line in the header of your PHP script:

use \Litipk\BigNumbers\Decimal as Decimal;

Constructors

The Decimal class provides many static constructors.

From any given value:

The most generic constructor, but also the slowest one.

Decimal::create($value, $scale=null)
  • $value : Any PHP native variable that represents a number.
  • $scale : The precision we want (measured in number of digits behind the fixed point).

From Integers

Decimal::fromInteger($intValue, $scale=null)

From Floating Point Numbers

Decimal::fromFloat($fltValue, $scale=null)

From Strings

Decimal::fromString($strValue, $scale=null)

From other Decimal objects

Constructs a new Decimal object based on a previous one, but changing it's $scale property.

Decimal::fromDecimal(Decimal $b, $scale=null)

Binary Operators

Remember that all Decimal objects are immutable, all operators return new instances rather than modify the object that called the method.

Addition

// c = a + b
$c = $a->add($b, $scale); // $a and $b are Decimal objects. $scale is optional.

Subtraction

// c = a - b
$c = $a->sub($b, $scale); // $a and $b are Decimal objects. $scale is optional.

Multiplication

// c = a · b
$c = $a->mul($b); // $a and $b are Decimal objects. $scale is optional.

Division

// c = a / b
$c = $a->div($b); // $a and $b are Decimal objects. $scale is optional.

Pow

// c = a^b
$c = $a->pow($b); // $a and $b are Decimal objects. $scale is optional.

Comparisons

Unary Operators

Square Root

Logarithms

Sign Operations

"Rounding"

Property Getters

TODO

PHP's Native Types Interaction

Clone this wiki locally