All Projects → PrestaShop → decimal

PrestaShop / decimal

Licence: MIT License
An object-oriented implementation of basic math operation with arbitrary precision, using BC Math if available.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to decimal

Rationals
🔟 Implementation of rational number arithmetic for .NET with arbitrary precision.
Stars: ✭ 40 (+185.71%)
Mutual labels:  arbitrary-precision
Decimal.js Light
The light version of decimal.js, an arbitrary-precision Decimal type for JavaScript.
Stars: ✭ 237 (+1592.86%)
Mutual labels:  arbitrary-precision
sey
Sey is a powerful math interpreter with infinite-precision.
Stars: ✭ 13 (-7.14%)
Mutual labels:  bcmath
Kotlin Multiplatform Bignum
A Kotlin multiplatform library for arbitrary precision arithmetics
Stars: ✭ 119 (+750%)
Mutual labels:  arbitrary-precision
Javascript Biginteger
A big integer library for JavaScript
Stars: ✭ 181 (+1192.86%)
Mutual labels:  arbitrary-precision
apint
Arbitrary precision integers library.
Stars: ✭ 23 (+64.29%)
Mutual labels:  arbitrary-precision
Lua Bint
Arbitrary precision integer arithmetic library in pure Lua
Stars: ✭ 20 (+42.86%)
Mutual labels:  arbitrary-precision
intx
intx – extended precision integer library
Stars: ✭ 83 (+492.86%)
Mutual labels:  arbitrary-precision
Tiny Bignum C
Small portable multiple-precision unsigned integer arithmetic in C
Stars: ✭ 224 (+1500%)
Mutual labels:  arbitrary-precision
decimal
An arbitrary-precision decimal floating-point arithmetic package for Go
Stars: ✭ 28 (+100%)
Mutual labels:  arbitrary-precision
Calc
C-style arbitrary precision calculator
Stars: ✭ 127 (+807.14%)
Mutual labels:  arbitrary-precision
Big Math
Advanced Java BigDecimal math functions (pow, sqrt, log, sin, ...) using arbitrary precision.
Stars: ✭ 173 (+1135.71%)
Mutual labels:  arbitrary-precision
math
Complex special functions and common mathematical operations in JavaScript
Stars: ✭ 42 (+200%)
Mutual labels:  arbitrary-precision
Physicalconstants.jl
Collection of fundamental physical constants with uncertainties. It supports arbitrary-precision constants
Stars: ✭ 55 (+292.86%)
Mutual labels:  arbitrary-precision
eapa
Erlang/Elixir Arbitrary-Precision Arithmetic (EAPA)
Stars: ✭ 36 (+157.14%)
Mutual labels:  arbitrary-precision
Math
Arbitrary-precision arithmetic library for PHP
Stars: ✭ 905 (+6364.29%)
Mutual labels:  arbitrary-precision
Measurements.jl
Error propagation calculator and library for physical measurements. It supports real and complex numbers with uncertainty, arbitrary precision calculations, operations with arrays, and numerical integration.
Stars: ✭ 248 (+1671.43%)
Mutual labels:  arbitrary-precision
bcmath-extended
Extends php BCMath lib for missing functions like floor, ceil, round, abs, min, max, rand for big numbers. Also wraps existing BCMath functions.
Stars: ✭ 59 (+321.43%)
Mutual labels:  arbitrary-precision
cldr-engine
Internationalization and localization in Typescript with Unicode CLDR, batteries included
Stars: ✭ 34 (+142.86%)
Mutual labels:  arbitrary-precision
hebimath
arbitrary precision arithmetic library
Stars: ✭ 37 (+164.29%)
Mutual labels:  arbitrary-precision

Decimal

Build status Coverage Status Total Downloads

An object-oriented BC Math extension wrapper/shim.

Decimal offers a stateless, fluent object-oriented implementation of basic arbitrary-precision arithmetic, using BC Math if available.

You can find out more about floating point precision here.

Example:

use PrestaShop\Decimal\DecimalNumber;
use PrestaShop\Decimal\Operation\Rounding;

echo (new DecimalNumber('0.1'))
    ->plus(new DecimalNumber('0.7'))
    ->times(new DecimalNumber('10'))
    ->round(0, Rounding::ROUND_FLOOR)
  
// echoes '8'

Install

Via Composer

$ composer require prestashop/decimal

Usage reference

Quick links:

Instantiation

Creates a new Decimal number.

public __construct ( string $number [, int $exponent = null ] ): DecimalNumber

There are two ways to instantiate a Decimal\DecimalNumber:

// create a number from string
$number = new PrestaShop\Decimal\DecimalNumber('123.456');
echo $number; // echoes '123.456'
// exponent notation
$number = new PrestaShop\Decimal\DecimalNumber('123456', -3);
echo $number; // echoes '123.456'

Addition

Returns the computed result of adding another number to the current one.

public DecimalNumber::plus ( DecimalNumber $addend ): DecimalNumber

Examples:

$a = new PrestaShop\Decimal\DecimalNumber('123.456');
$b = new PrestaShop\Decimal\DecimalNumber('654.321');

echo $a->plus($b); // echoes '777.777'

Subtraction

Returns the computed result of subtracting another number to the current one.

public DecimalNumber::minus ( DecimalNumber $subtrahend ): DecimalNumber

Examples:

$a = new PrestaShop\Decimal\DecimalNumber('777.777');
$b = new PrestaShop\Decimal\DecimalNumber('654.321');

echo $a->minus($b); // echoes '123.456'

Multiplication

Returns the computed result of multiplying the current number with another one.

public DecimalNumber::times ( DecimalNumber $factor ): DecimalNumber

Examples:

$a = new PrestaShop\Decimal\DecimalNumber('777.777');
$b = new PrestaShop\Decimal\DecimalNumber('654.321');

echo $a->times($b); // echoes '508915.824417'

Division

Returns the computed result of dividing the current number by another one, with up to a certain number of decimal positions (6 by default).

public DecimalNumber::dividedBy ( DecimalNumber $divisor [, int $precision = Operation\Division::DEFAULT_PRECISION ] )

Examples:

$a = new PrestaShop\Decimal\DecimalNumber('777.777');
$b = new PrestaShop\Decimal\DecimalNumber('654.321');

echo $a->dividedBy($b, 0);  // echoes '1'
echo $a->dividedBy($b, 5);  // echoes '1.18867'
echo $a->dividedBy($b, 10); // echoes '1.1886780341'
echo $a->dividedBy($b, 15); // echoes '1.188678034175886'

Comparison

Returns the result of the comparison assertion.

$a = new PrestaShop\Decimal\DecimalNumber('777.777');
$b = new PrestaShop\Decimal\DecimalNumber('654.321');

$a->equals($b);                // returns false
$a->isLowerThan($b);           // returns false
$a->isLowerOrEqualThan($b);    // returns false
$a->isGreaterThan($b);         // returns true
$a->isGreaterOrEqualThan($b);  // returns true

// shortcut methods
$a->equalsZero();               // returns false
$a->isLowerThanZero();          // returns false 
$a->isLowerOrEqualThanZero();   // returns false
$a->isGreaterThanZero();        // returns true
$a->isGreaterOrEqualThanZero(); // returns true

Fixed precision

Returns the number as a string, optionally rounded, with an exact number of decimal positions.

public DecimalNumber::toPrecision ( int $precision [, string $roundingMode = Rounding::ROUND_TRUNCATE ] ): string

Examples:

$a = new PrestaShop\Decimal\DecimalNumber('123.456');
$a = new PrestaShop\Decimal\DecimalNumber('-123.456');

// truncate / pad
$a->toPrecision(0); // '123'
$a->toPrecision(1); // '123.4'
$a->toPrecision(2); // '123.45'
$a->toPrecision(3); // '123.456'
$a->toPrecision(4); // '123.4560'
$b->toPrecision(0); // '-123'
$b->toPrecision(1); // '-123.4'
$b->toPrecision(2); // '-123.45'
$b->toPrecision(3); // '-123.456'
$b->toPrecision(4); // '-123.4560'

// ceil (round up)
$a->toPrecision(0, PrestaShop\Decimal\Operation\Rounding::ROUND_CEIL); // '124'
$a->toPrecision(1, PrestaShop\Decimal\Operation\Rounding::ROUND_CEIL); // '123.5'
$a->toPrecision(2, PrestaShop\Decimal\Operation\Rounding::ROUND_CEIL); // '123.46'
$b->toPrecision(0, PrestaShop\Decimal\Operation\Rounding::ROUND_CEIL); // '-122'
$b->toPrecision(1, PrestaShop\Decimal\Operation\Rounding::ROUND_CEIL); // '-123.3'
$b->toPrecision(2, PrestaShop\Decimal\Operation\Rounding::ROUND_CEIL); // '-123.44'

// floor (round down)
$a->toPrecision(0, PrestaShop\Decimal\Operation\Rounding::ROUND_FLOOR); // '123'
$a->toPrecision(1, PrestaShop\Decimal\Operation\Rounding::ROUND_FLOOR); // '123.4'
$a->toPrecision(2, PrestaShop\Decimal\Operation\Rounding::ROUND_FLOOR); // '123.45'
$b->toPrecision(0, PrestaShop\Decimal\Operation\Rounding::ROUND_FLOOR); // '-124'
$b->toPrecision(1, PrestaShop\Decimal\Operation\Rounding::ROUND_FLOOR); // '-123.5'
$b->toPrecision(2, PrestaShop\Decimal\Operation\Rounding::ROUND_FLOOR); // '-123.46'

// half-up (symmetric half-up)
$a->toPrecision(0, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_UP); // '123'
$a->toPrecision(1, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_UP); // '123.5'
$a->toPrecision(2, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_UP); // '123.46'
$b->toPrecision(0, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_UP); // '-123'
$b->toPrecision(1, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_UP); // '-123.5'
$b->toPrecision(2, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_UP); // '-123.46'

// half-down (symmetric half-down)
$a->toPrecision(0, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_DOWN); // '123'
$a->toPrecision(1, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_DOWN); // '123.4'
$a->toPrecision(2, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_DOWN); // '123.46'
$a->toPrecision(0, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_DOWN); // '-123'
$a->toPrecision(1, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_DOWN); // '-123.4'
$a->toPrecision(2, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_DOWN); // '-123.46'

// half-even
$a->toPrecision(0, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN); // '123'
$a->toPrecision(1, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN); // '123.4'
$a->toPrecision(2, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN); // '123.46'
$a->toPrecision(0, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN); // '-123'
$a->toPrecision(1, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN); // '-123.4'
$a->toPrecision(2, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN); // '-123.46'

$a = new PrestaShop\Decimal\DecimalNumber('1.1525354556575859505');
$a->toPrecision(0, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN);  // '1'
$a->toPrecision(1, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN);  // '1.2'
$a->toPrecision(2, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN);  // '1.15'
$a->toPrecision(3, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN);  // '1.152'
$a->toPrecision(4, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN);  // '1.1525'
$a->toPrecision(5, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN);  // '1.15255'
$a->toPrecision(6, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN);  // '1.152535'
$a->toPrecision(7, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN);  // '1.1525354'
$a->toPrecision(8, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN);  // '1.15253546'
$a->toPrecision(9, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN);  // '1.152535456'
$a->toPrecision(10, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN); // '1.1525354556'

Rounding

Rounding behaves like toPrecision, but provides "up to" a certain number of decimal positions (it does not add trailing zeroes).

public DecimalNumber::round ( int $maxDecimals [, string $roundingMode = Rounding::ROUND_TRUNCATE ] ): string

Examples:

$a = new PrestaShop\Decimal\DecimalNumber('123.456');
$a = new PrestaShop\Decimal\DecimalNumber('-123.456');

// truncate / pad
$a->round(0); // '123'
$a->round(1); // '123.4'
$a->round(2); // '123.45'
$a->round(3); // '123.456'
$a->round(4); // '123.456'
$b->round(0); // '-123'
$b->round(1); // '-123.4'
$b->round(2); // '-123.45'
$b->round(3); // '-123.456'
$b->round(4); // '-123.456'

Dot shifting

Creates a new copy of this number multiplied by 10^exponent

public DecimalNumber::toMagnitude ( int $exponent ): DecimalNumber

Examples:

$a = new PrestaShop\Decimal\DecimalNumber('123.456789');

// shift 3 digits to the left
$a->toMagnitude(-3); // 0.123456789

// shift 3 digits to the right
$a->toMagnitude(3); // 123456.789

Useful methods

$number = new PrestaShop\Decimal\DecimalNumber('123.45');
$number->getIntegerPart();    // '123'
$number->getFractionalPart(); // '45'
$number->getPrecision();      // '2' (number of decimals)
$number->getSign();           // '' ('-' if the number was negative)
$number->getExponent();       // '2' (always positive)
$number->getCoefficient();    // '123456'
$number->isPositive();        // true
$number->isNegative();        // false
$number->invert();            // new Decimal\DecimalNumber('-123.45')

Testing

$ composer install
$ vendor/bin/phpunit

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].