Download the PHP package penangites/number without Composer
On this page you can find all versions of the php package penangites/number. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download penangites/number
More information about penangites/number
Files in penangites/number
Package number
Short Description Immutable, chainable Number and Percentage value objects with exact bcmath arithmetic.
License MIT
Homepage https://github.com/penangites/number
Informations about the package number
Number
An immutable, chainable Number value object for PHP with exact bcmath
arithmetic — plus a Percentage companion. No runtime dependencies beyond
bcmath, no floating-point arithmetic, no drift.
After construction, addition, subtraction, multiplication and percentage
operations are exact at any precision. Only divide() and round() discard
digits — and they take an explicit scale and rounding mode, so precision is
never lost silently.
For Laravel integration (Eloquent casts, validation), see
penangites/laravel-number.
Installation
Requires PHP 8.3+ and the bcmath extension.
Usage
Creating numbers
Use strings for decimal values when every supplied digit must be preserved.
A float is stored as the shortest decimal that reads back as the same double, so nothing is invented and nothing is dropped — but a float has already lost whatever it lost before the call, and that shows:
Scientific notation is accepted wherever a decimal string is — Percentage
included — because it is what a float cast, a JSON decode or a database driver
hands back. It is expanded positionally, so '1.0E-5' and '0.00001' are the
same value. Exponents beyond ±100000 are refused: a value needing more digits
than that should be written out in full, where the input is as large as the
result.
Reading values
toInt() only converts losslessly: it throws on fractional values instead of
silently truncating.
Arithmetic
Every operation returns a new instance — a Number never changes. Operands
can be another Number, an int, a string, or a float.
Division and rounding
Division is the one operation that can produce endless digits, so it takes an
explicit scale (default 12) and a RoundingMode (default half away from zero):
Modes: HalfAwayFromZero (default), Up, Down, Ceiling, Floor. Rounding
uses the exact remainder, so every mode is correct even when the discarded
digits lie far beyond the scale.
Values are returned in canonical form without trailing zeros. The scale limits decimal precision; it does not format or pad the result:
Percentages
A Percentage has no string cast: "0.06" and "6" are both plausible, so ask
for toPercent(), toRatio() or toArray() by name rather than have the wrong
one render silently.
Percentage supports exact add, subtract, negate, comparisons and sign
checks of its own:
Apply one to a Number:
increaseBy / decreaseBy are the general-purpose names for tax /
discount. All percentage operations are exact — chain ->round() when you
need to limit decimal precision.
A complete price calculation can stay exact until the final rounding step:
Comparison
Recipes
Spelling a number out in words
Deliberately not part of this package — PHP's intl extension already does it
in every CLDR locale, so use it directly:
Testing
This runs code style checks (Pint), static analysis (PHPStan at level 10, the maximum) and the Pest test suite.
Changelog
See CHANGELOG for what has changed recently.
Contributing
See CONTRIBUTING for details.
Security
If you discover a security issue, please read our security policy — do not use the issue tracker.
License
MIT — see LICENSE.
All versions of number with dependencies
ext-bcmath Version *