{"id":15018912,"url":"https://github.com/prestashop/decimal","last_synced_at":"2025-10-17T13:56:46.050Z","repository":{"id":44648759,"uuid":"98204890","full_name":"PrestaShop/decimal","owner":"PrestaShop","description":"An object-oriented implementation of basic math operation with arbitrary precision, using BC Math if available.","archived":false,"fork":false,"pushed_at":"2022-10-26T15:23:33.000Z","size":90,"stargazers_count":17,"open_issues_count":1,"forks_count":8,"subscribers_count":12,"default_branch":"develop","last_synced_at":"2024-10-29T17:04:12.470Z","etag":null,"topics":["arbitrary-precision","bcmath","core-dependency","hacktoberfest","php","php-library"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PrestaShop.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-07-24T15:18:01.000Z","updated_at":"2023-07-25T06:40:38.000Z","dependencies_parsed_at":"2022-09-25T00:10:14.776Z","dependency_job_id":null,"html_url":"https://github.com/PrestaShop/decimal","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PrestaShop%2Fdecimal","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PrestaShop%2Fdecimal/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PrestaShop%2Fdecimal/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PrestaShop%2Fdecimal/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PrestaShop","download_url":"https://codeload.github.com/PrestaShop/decimal/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237172108,"owners_count":19266614,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arbitrary-precision","bcmath","core-dependency","hacktoberfest","php","php-library"],"created_at":"2024-09-24T19:52:36.805Z","updated_at":"2025-10-17T13:56:41.016Z","avatar_url":"https://github.com/PrestaShop.png","language":"PHP","readme":"# Decimal\n\n[![Build status](https://github.com/PrestaShop/decimal/actions/workflows/php.yml/badge.svg)](https://github.com/PrestaShop/decimal/actions/workflows/php.yml)\n[![Coverage Status](https://coveralls.io/repos/github/PrestaShop/decimal/badge.svg)](https://coveralls.io/github/PrestaShop/decimal)\n[![Total Downloads](https://img.shields.io/packagist/dt/prestashop/decimal.svg?style=flat-square)](https://packagist.org/packages/prestashop/decimal)\n\nAn object-oriented [BC Math extension](http://php.net/manual/en/book.bc.php) wrapper/shim.\n\n**Decimal** offers a stateless, fluent object-oriented implementation of basic arbitrary-precision arithmetic, using BC Math if available.\n\nYou can find out more about floating point precision [here](http://php.net/float).\n\nExample:\n```php\nuse PrestaShop\\Decimal\\DecimalNumber;\nuse PrestaShop\\Decimal\\Operation\\Rounding;\n\necho (new DecimalNumber('0.1'))\n    -\u003eplus(new DecimalNumber('0.7'))\n    -\u003etimes(new DecimalNumber('10'))\n    -\u003eround(0, Rounding::ROUND_FLOOR)\n  \n// echoes '8'\n```\n\n## Install\n\nVia Composer\n\n``` bash\n$ composer require prestashop/decimal\n```\n\n## Usage reference\n\nQuick links:\n- [Instantiation](#instantiation)\n- [Addition](#addition)\n- [Subtraction](#subtraction)\n- [Multiplication](#multiplication)\n- [Division](#division)\n- [Comparison](#comparison)\n- [Fixed precision](#fixed-precision)\n- [Rounding](#rounding)\n- [Dot shifting](#dot-shifting)\n- [Useful methods](#useful-methods)\n\n### Instantiation\nCreates a new Decimal number.\n```php\npublic __construct ( string $number [, int $exponent = null ] ): DecimalNumber\n```\nThere are two ways to instantiate a Decimal\\DecimalNumber:\n``` php\n// create a number from string\n$number = new PrestaShop\\Decimal\\DecimalNumber('123.456');\necho $number; // echoes '123.456'\n```\n``` php\n// exponent notation\n$number = new PrestaShop\\Decimal\\DecimalNumber('123456', -3);\necho $number; // echoes '123.456'\n```\n\n### Addition\nReturns the computed result of adding another number to the current one.\n```php\npublic DecimalNumber::plus ( DecimalNumber $addend ): DecimalNumber\n```\nExamples:\n```php\n$a = new PrestaShop\\Decimal\\DecimalNumber('123.456');\n$b = new PrestaShop\\Decimal\\DecimalNumber('654.321');\n\necho $a-\u003eplus($b); // echoes '777.777'\n```\n\n### Subtraction\nReturns the computed result of subtracting another number to the current one.\n```php\npublic DecimalNumber::minus ( DecimalNumber $subtrahend ): DecimalNumber\n```\nExamples:\n```php\n$a = new PrestaShop\\Decimal\\DecimalNumber('777.777');\n$b = new PrestaShop\\Decimal\\DecimalNumber('654.321');\n\necho $a-\u003eminus($b); // echoes '123.456'\n```\n\n### Multiplication\nReturns the computed result of multiplying the current number with another one.\n```php\npublic DecimalNumber::times ( DecimalNumber $factor ): DecimalNumber\n```\nExamples:\n```php\n$a = new PrestaShop\\Decimal\\DecimalNumber('777.777');\n$b = new PrestaShop\\Decimal\\DecimalNumber('654.321');\n\necho $a-\u003etimes($b); // echoes '508915.824417'\n```\n\n### Division\nReturns the computed result of dividing  the current number by another one, with up to a certain number of decimal positions (6 by default).\n```php\npublic DecimalNumber::dividedBy ( DecimalNumber $divisor [, int $precision = Operation\\Division::DEFAULT_PRECISION ] )\n```\nExamples:\n```php\n$a = new PrestaShop\\Decimal\\DecimalNumber('777.777');\n$b = new PrestaShop\\Decimal\\DecimalNumber('654.321');\n\necho $a-\u003edividedBy($b, 0);  // echoes '1'\necho $a-\u003edividedBy($b, 5);  // echoes '1.18867'\necho $a-\u003edividedBy($b, 10); // echoes '1.1886780341'\necho $a-\u003edividedBy($b, 15); // echoes '1.188678034175886'\n```\n\n### Comparison\nReturns the result of the comparison assertion.\n```php\n$a = new PrestaShop\\Decimal\\DecimalNumber('777.777');\n$b = new PrestaShop\\Decimal\\DecimalNumber('654.321');\n\n$a-\u003eequals($b);                // returns false\n$a-\u003eisLowerThan($b);           // returns false\n$a-\u003eisLowerOrEqualThan($b);    // returns false\n$a-\u003eisGreaterThan($b);         // returns true\n$a-\u003eisGreaterOrEqualThan($b);  // returns true\n\n// shortcut methods\n$a-\u003eequalsZero();               // returns false\n$a-\u003eisLowerThanZero();          // returns false \n$a-\u003eisLowerOrEqualThanZero();   // returns false\n$a-\u003eisGreaterThanZero();        // returns true\n$a-\u003eisGreaterOrEqualThanZero(); // returns true\n```\n\n### Fixed precision\nReturns the number as a string, optionally rounded, with an exact number of decimal positions.\n```php\npublic DecimalNumber::toPrecision ( int $precision [, string $roundingMode = Rounding::ROUND_TRUNCATE ] ): string\n```\nExamples:\n```php\n$a = new PrestaShop\\Decimal\\DecimalNumber('123.456');\n$a = new PrestaShop\\Decimal\\DecimalNumber('-123.456');\n\n// truncate / pad\n$a-\u003etoPrecision(0); // '123'\n$a-\u003etoPrecision(1); // '123.4'\n$a-\u003etoPrecision(2); // '123.45'\n$a-\u003etoPrecision(3); // '123.456'\n$a-\u003etoPrecision(4); // '123.4560'\n$b-\u003etoPrecision(0); // '-123'\n$b-\u003etoPrecision(1); // '-123.4'\n$b-\u003etoPrecision(2); // '-123.45'\n$b-\u003etoPrecision(3); // '-123.456'\n$b-\u003etoPrecision(4); // '-123.4560'\n\n// ceil (round up)\n$a-\u003etoPrecision(0, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_CEIL); // '124'\n$a-\u003etoPrecision(1, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_CEIL); // '123.5'\n$a-\u003etoPrecision(2, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_CEIL); // '123.46'\n$b-\u003etoPrecision(0, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_CEIL); // '-122'\n$b-\u003etoPrecision(1, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_CEIL); // '-123.3'\n$b-\u003etoPrecision(2, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_CEIL); // '-123.44'\n\n// floor (round down)\n$a-\u003etoPrecision(0, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_FLOOR); // '123'\n$a-\u003etoPrecision(1, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_FLOOR); // '123.4'\n$a-\u003etoPrecision(2, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_FLOOR); // '123.45'\n$b-\u003etoPrecision(0, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_FLOOR); // '-124'\n$b-\u003etoPrecision(1, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_FLOOR); // '-123.5'\n$b-\u003etoPrecision(2, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_FLOOR); // '-123.46'\n\n// half-up (symmetric half-up)\n$a-\u003etoPrecision(0, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_HALF_UP); // '123'\n$a-\u003etoPrecision(1, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_HALF_UP); // '123.5'\n$a-\u003etoPrecision(2, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_HALF_UP); // '123.46'\n$b-\u003etoPrecision(0, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_HALF_UP); // '-123'\n$b-\u003etoPrecision(1, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_HALF_UP); // '-123.5'\n$b-\u003etoPrecision(2, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_HALF_UP); // '-123.46'\n\n// half-down (symmetric half-down)\n$a-\u003etoPrecision(0, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_HALF_DOWN); // '123'\n$a-\u003etoPrecision(1, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_HALF_DOWN); // '123.4'\n$a-\u003etoPrecision(2, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_HALF_DOWN); // '123.46'\n$a-\u003etoPrecision(0, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_HALF_DOWN); // '-123'\n$a-\u003etoPrecision(1, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_HALF_DOWN); // '-123.4'\n$a-\u003etoPrecision(2, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_HALF_DOWN); // '-123.46'\n\n// half-even\n$a-\u003etoPrecision(0, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_HALF_EVEN); // '123'\n$a-\u003etoPrecision(1, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_HALF_EVEN); // '123.4'\n$a-\u003etoPrecision(2, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_HALF_EVEN); // '123.46'\n$a-\u003etoPrecision(0, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_HALF_EVEN); // '-123'\n$a-\u003etoPrecision(1, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_HALF_EVEN); // '-123.4'\n$a-\u003etoPrecision(2, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_HALF_EVEN); // '-123.46'\n\n$a = new PrestaShop\\Decimal\\DecimalNumber('1.1525354556575859505');\n$a-\u003etoPrecision(0, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_HALF_EVEN);  // '1'\n$a-\u003etoPrecision(1, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_HALF_EVEN);  // '1.2'\n$a-\u003etoPrecision(2, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_HALF_EVEN);  // '1.15'\n$a-\u003etoPrecision(3, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_HALF_EVEN);  // '1.152'\n$a-\u003etoPrecision(4, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_HALF_EVEN);  // '1.1525'\n$a-\u003etoPrecision(5, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_HALF_EVEN);  // '1.15255'\n$a-\u003etoPrecision(6, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_HALF_EVEN);  // '1.152535'\n$a-\u003etoPrecision(7, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_HALF_EVEN);  // '1.1525354'\n$a-\u003etoPrecision(8, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_HALF_EVEN);  // '1.15253546'\n$a-\u003etoPrecision(9, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_HALF_EVEN);  // '1.152535456'\n$a-\u003etoPrecision(10, PrestaShop\\Decimal\\Operation\\Rounding::ROUND_HALF_EVEN); // '1.1525354556'\n```\n\n### Rounding\nRounding behaves like `toPrecision`, but provides \"up to\" a certain number of decimal positions\n(it does not add trailing zeroes).\n```php\npublic DecimalNumber::round ( int $maxDecimals [, string $roundingMode = Rounding::ROUND_TRUNCATE ] ): string\n```\nExamples:\n```php\n$a = new PrestaShop\\Decimal\\DecimalNumber('123.456');\n$a = new PrestaShop\\Decimal\\DecimalNumber('-123.456');\n\n// truncate / pad\n$a-\u003eround(0); // '123'\n$a-\u003eround(1); // '123.4'\n$a-\u003eround(2); // '123.45'\n$a-\u003eround(3); // '123.456'\n$a-\u003eround(4); // '123.456'\n$b-\u003eround(0); // '-123'\n$b-\u003eround(1); // '-123.4'\n$b-\u003eround(2); // '-123.45'\n$b-\u003eround(3); // '-123.456'\n$b-\u003eround(4); // '-123.456'\n```\n\n### Dot shifting\nCreates a new copy of this number multiplied by 10^exponent\n```php\npublic DecimalNumber::toMagnitude ( int $exponent ): DecimalNumber\n```\nExamples:\n```php\n$a = new PrestaShop\\Decimal\\DecimalNumber('123.456789');\n\n// shift 3 digits to the left\n$a-\u003etoMagnitude(-3); // 0.123456789\n\n// shift 3 digits to the right\n$a-\u003etoMagnitude(3); // 123456.789\n```\n\n### Useful methods\n```php\n$number = new PrestaShop\\Decimal\\DecimalNumber('123.45');\n$number-\u003egetIntegerPart();    // '123'\n$number-\u003egetFractionalPart(); // '45'\n$number-\u003egetPrecision();      // '2' (number of decimals)\n$number-\u003egetSign();           // '' ('-' if the number was negative)\n$number-\u003egetExponent();       // '2' (always positive)\n$number-\u003egetCoefficient();    // '123456'\n$number-\u003eisPositive();        // true\n$number-\u003eisNegative();        // false\n$number-\u003einvert();            // new Decimal\\DecimalNumber('-123.45')\n```\n\n## Testing\n\n``` bash\n$ composer install\n$ vendor/bin/phpunit\n```\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n## Credits\n\n- [All Contributors](https://github.com/prestashop/decimal/contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprestashop%2Fdecimal","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprestashop%2Fdecimal","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprestashop%2Fdecimal/lists"}