Download the PHP package jedstrom/php-decimal without Composer
On this page you can find all versions of the php package jedstrom/php-decimal. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jedstrom/php-decimal
More information about jedstrom/php-decimal
Files in jedstrom/php-decimal
Package php-decimal
Short Description PHP Decimal Type implementation
License BSD-3-Clause
Informations about the package php-decimal
jedstrom/php-decimal
A PHP Decimal Type implementation
A value object class that utilizes BCMath functions to perform arbitrary precision arithmetic operations.
Usage
Supported Operations
The following operations are currently supported:
- Addition
- Subtraction
- Multiplication
- Division
Source
$termOne = new Decimal('2', 2);
$termTwo = new Decimal('3', 0);
var_dump($termOne->add($termTwo)->getValue());
var_dump($termOne->subtract($termTwo)->getValue());
var_dump($termOne->multiply($termTwo)->getValue());
var_dump($termOne->divide($termTwo)->getValue());
// refer to Mixed Precision Calculations section
var_dump($termTwo->add($termOne)->getValue());
var_dump($termTwo->subtract($termOne)->getValue());
var_dump($termTwo->multiply($termOne)->getValue());
var_dump($termTwo->divide($termOne)->getValue());
Output
string(4) "5.00"
string(4) "-1.00"
string(4) "6.00"
string(4) "0.66"
string(4) "5"
string(4) "1"
string(4) "6"
string(4) "2"
Automatic Precision Determination
If the precision is not explicitly set, the precision required to represent the value will be automatically calculated.
Source
$decimal = new Decimal('3.14159');
var_dump($decimal->getPrecision());
Output
int(5)
Rounding Methodology
Rounding is performed using the half away from zero method.
Source
$termOne = new Decimal('1');
$termTwo = new Decimal('1.5');
$product = $termOne->multiply($termTwo);
var_dump($product->getValue());
Output
string(1) "2"
Source
$termOne = new Decimal('-1');
$termTwo = new Decimal('1.5');
$product = $termOne->multiply($termTwo);
var_dump($product->getValue());
Output
string(1) "-2"
Precision Truncation
If a precision is explicitly specified and the value specified requires more precision than specified, the value will be rounded to the specified precision.
Source
$decimal = new Decimal('3.14159', 3);
var_dump($decimal->getValue());
Output
string(5) "3.142"
Mixed Precision Calculations
Calculations are performed internally at the maximum precision of either term + 1 and then rounded to the precision of the first term.
So, in the following example:
19.99 x 0.07125
The internal result is:
1.424287
And the output is rounded to the precision of the first term (19.99 => 2) to produce:
1.42
Source
$price = new Decimal('19.99', 2);
$taxRate = new Decimal('0.07125', 5');
$tax = $price->multiply($taxRate);
var_dump($tax);
Output
string(4) "1.42"All versions of php-decimal with dependencies
ext-bcmath Version *