Download the PHP package jimeneztdavid/scaled-int without Composer
On this page you can find all versions of the php package jimeneztdavid/scaled-int. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jimeneztdavid/scaled-int
More information about jimeneztdavid/scaled-int
Files in jimeneztdavid/scaled-int
Package scaled-int
Short Description A value object for scaled integers (e.g. money cents) with safe arithmetic and configurable rounding.
License MIT
Informations about the package scaled-int
ScaledInt
Why this package exists
Working with money, prices, weights, percentages, or any decimal-based value using floating-point numbers (float) is dangerous.
Rounding errors, precision loss, and unexpected comparisons are common problems in real-world applications.
ScaledInt solves this by:
- Storing values internally as integers only
- Applying a fixed scale (power of 10) to represent decimals
- Providing safe arithmetic, comparisons, and rounding
- Avoiding floating-point math entirely
This makes it ideal for:
- Money and prices
- Taxes (IVA / VAT)
- Percentages and discounts
- Measurements (kg, grams, meters)
- Any domain where precision matters
Core concept
A ScaledInt stores:
- minor → integer value (scaled)
- scale → power of 10 (10, 100, 1000, etc.)
Example with scale 100:
| Human value | Stored minor |
|---|---|
10.25 |
1025 |
5.00 |
500 |
-3.50 |
-350 |
Installation
Creating values
From a major (human) value
From a minor (integer) value
Using a custom scale
Scale must be a power of 10 (10, 100, 1000, ...)
Reading values
Get minor value
Get scale
Convert back to string
Comparisons
All comparisons require the same scale.
compareTo()
Use compareTo() when you want a single method that tells you the ordering between two ScaledInt values.
- Returns -1 if this value is less than the other
- Returns 0 if both values are equal
- Returns 1 if this value is greater than the other
Both numbers must have the same scale, otherwise an InvalidArgumentException is thrown.
Example
min() and max()
Use min() to get the smaller value and max() to get the larger value.
Both values must have the same scale.
between()
Use between() to check whether a value is inside an inclusive range.
Both bounds and the value must have the same scale.
Arithmetic operations
Addition
Subtraction
Multiply by integer
Divide by integer (exact)
Throws if the division is not exact.
Division with rounding
Supported modes:
UPDOWNHALF_UPHALF_EVEN
Percentages
Common scenarios
Calculate IVA (VAT)
Extract IVA from a final price
IVA included price formula:
Example with 19% IVA:
Discounts
Measurements (non-money)
Why not floats?
Design principles
- Immutable objects
- Integer-only math
- Explicit rounding
- Overflow-safe operations
- Scale consistency enforced
When to use ScaledInt
Financial calculations
Taxes and percentages
Measurements
Precise comparisons
Scientific floating-point math
Values requiring arbitrary precision decimals
Float vs ScaledInt
| Aspect | float |
ScaledInt |
|---|---|---|
| Precision | Imprecise (binary rounding errors) | Exact (integer math) |
| Internal representation | IEEE 754 binary floating point | Integer + fixed scale |
0.1 + 0.2 |
0.30000000000000004 |
"0.30" |
| Equality comparison | Unreliable | Safe and deterministic |
| Rounding control | Implicit and inconsistent | Explicit (UP, DOWN, HALF_UP, HALF_EVEN) |
| Overflow detection | Silent | Explicit exceptions |
| Currency safety | Dangerous | Designed for money |
| Percentage calculations | Error-prone | Deterministic |
Comparisons (> < ==) |
Risky | Guaranteed correctness |
| Domain intent | Generic numeric | Explicit domain modeling |
| Production suitability | Needs extra care | Safe by default |
License
MIT