Skip to content

Commit d6dbabb

Browse files
committed
Document meaning of scale
1 parent 5f2598d commit d6dbabb

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

docs/README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Transformation:
3636
These return a new object:
3737
- `absolute()`: Returns the absolute (positive) value of this decimal.
3838
- `negate()`: Returns the negation (positive if negative and vice versa).
39-
- `trim()`: Remove trailing zeros after the comma (same value, but different semantic meaning in term of precision/scale).
39+
- `trim()`: Remove trailing zeros after the comma (same value, but different semantic meaning in terms of precision/scale).
4040

4141
There is only one static method and acts as a convenience wrapper to create an object:
4242
- `create()`: Internally does `new Decimal($value)`, allows for easier chaining without need of `()` wrapping.
@@ -81,20 +81,26 @@ $decimalAdded = $decimalOne->add($decimalTwo); // Now '3.3'
8181
Note that due to immutability `$decimalOne` is not modified here. The re-assignment is necessary for the operation to persist.
8282

8383
### Precision/Scale
84-
Operations like `add()` use the higher of the scales of the operands.
84+
Operations like `add()` use the higher one of the scales of the operands.
8585
If both are the same, they would also stay the same.
8686

87-
With other operations like `multiply()`, they scale would be the addition of both operands' scale:
87+
With other operations like `multiply()`, the scale would be the addition of both operands' scale by default:
8888
```php
8989
$decimalOne = Decimal::create('1.55');
9090
$decimalTwo = Decimal::create('2.00');
9191

9292
echo $decimalOne->multiply($decimalTwo); // Prints '3.1000'
93+
```
94+
95+
To produce a result with a different number of decimal places, provide a value for the `$scale` parameter:
96+
```php
97+
$decimalOne = Decimal::create('1.55');
98+
$decimalTwo = Decimal::create('2.00');
9399

94-
// Keeping 2 digit scale requires a 2nd argument
95100
echo $decimalOne->multiply($decimalTwo, 2); // Prints '3.10'
96101
```
97102

103+
98104
## Contributing
99105

100106
You can contribute as pull request directly:

src/Decimal.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,15 @@ class Decimal implements JsonSerializable, Stringable
5656
protected bool $negative = false;
5757

5858
/**
59+
* Decimal places to be applied to results
60+
*
5961
* decimal(10,6) => 6
6062
*/
6163
protected int $scale = 0;
6264

6365
/**
6466
* @param object|string|float|int $value
65-
* @param int|null $scale Leave empty to auto-detect.
67+
* @param int|null $scale Decimal places in the result. Omit to enable auto-detection.
6668
*/
6769
public function __construct(object|string|float|int $value, ?int $scale = null)
6870
{
@@ -143,7 +145,7 @@ protected function normalizeValue(string $value): string
143145
* it.
144146
*
145147
* @param object|string|float|int $value
146-
* @param int|null $scale Leave empty to auto-detect.
148+
* @param int|null $scale Decimal places in the result. Omit to enable auto-detection.
147149
*
148150
* @return static
149151
*/
@@ -247,7 +249,7 @@ public function compareTo(self|string|float|int $value): int
247249
* Add $value to this Decimal and return the sum as a new Decimal.
248250
*
249251
* @param static|string|float|int $value
250-
* @param int|null $scale Leave empty to auto-detect.
252+
* @param int|null $scale Decimal places in the result. Omit to enable auto-detection.
251253
*
252254
* @return static
253255
*/
@@ -281,7 +283,7 @@ protected function resultScale(self $a, self $b, ?int $scale = null): int
281283
* Decimal.
282284
*
283285
* @param static|string|float|int $value
284-
* @param int|null $scale Leave empty to auto-detect.
286+
* @param int|null $scale Decimal places in the result. Omit to enable auto-detection.
285287
*
286288
* @return static
287289
*/
@@ -389,7 +391,7 @@ public function isPositive(): bool
389391
* Multiply this Decimal by $value and return the product as a new Decimal.
390392
*
391393
* @param static|string|float|int $value
392-
* @param int|null $scale Leave empty to auto-detect.
394+
* @param int|null $scale Decimal places in the result. Omit to enable auto-detection.
393395
*
394396
* @return static
395397
*/
@@ -407,7 +409,7 @@ public function multiply(self|string|int|float $value, ?int $scale = null): stat
407409
* Divide this Decimal by $value and return the quotient as a new Decimal.
408410
*
409411
* @param static|string|float|int $value
410-
* @param int $scale
412+
* @param int $scale Decimal places in the result
411413
*
412414
* @throws \DivisionByZeroError if $value is zero.
413415
*
@@ -427,7 +429,7 @@ public function divide(self|string|int|float $value, int $scale): static
427429
* This method is equivalent to the ** operator.
428430
*
429431
* @param static|string|int $exponent
430-
* @param int|null $scale Leave empty to use current.
432+
* @param int|null $scale Decimal places in the result. Omit to enable auto-detection.
431433
*
432434
* @return static
433435
*/
@@ -443,7 +445,7 @@ public function pow(self|string|int $exponent, ?int $scale = null): static
443445
/**
444446
* Returns the square root of this decimal, with the same scale as this decimal.
445447
*
446-
* @param int|null $scale Leave empty to use current.
448+
* @param int|null $scale Decimal places in the result. Omit to enable auto-detection.
447449
*
448450
* @return static
449451
*/
@@ -460,7 +462,7 @@ public function sqrt(?int $scale = null): static
460462
* This method is equivalent to the % operator.
461463
*
462464
* @param static|string|int $value
463-
* @param int|null $scale Leave empty to use current.
465+
* @param int|null $scale Decimal places in the result. Omit to enable auto-detection.
464466
*
465467
* @return static
466468
*/
@@ -474,7 +476,7 @@ public function mod(self|string|int $value, ?int $scale = null): static
474476
}
475477

476478
/**
477-
* @param int $scale
479+
* @param int $scale Decimal places in the result (only used with mode "half up")
478480
* @param int $roundMode
479481
*
480482
* @return static
@@ -524,7 +526,7 @@ public function ceil(): static
524526
/**
525527
* The result of discarding all digits behind the defined scale.
526528
*
527-
* @param int $scale
529+
* @param int $scale Decimal places in the result
528530
*
529531
* @throws \InvalidArgumentException
530532
*

0 commit comments

Comments
 (0)