Skip to content

Commit 6b3aa8a

Browse files
committed
fix: Fixes scaling when getting primitive values for BigNumbers.
1 parent b4f8cb5 commit 6b3aa8a

File tree

5 files changed

+64
-14
lines changed

5 files changed

+64
-14
lines changed

infection.json.dist

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
"BCMath": false,
1818
"CastInt": false,
1919
"Ternary": false,
20-
"MethodCallRemoval": false,
2120
"ProtectedVisibility": false
2221
},
2322
"phpUnit": {

src/Internal/Scale.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@ public function scaleOf(string $value): Scale
4040

4141
public function numberWithScale(Number $number, int $scale): Number
4242
{
43-
$result = explode('.', $number->value);
44-
45-
if (count($result) <= 1) {
46-
return Number::from(value: $result[0]);
43+
if ($number->isZero()) {
44+
$formattedValue = number_format(0, $scale, '.', '');
45+
return Number::from(value: $formattedValue);
4746
}
4847

48+
$result = explode('.', $number->value);
4949
$decimal = $result[0];
50-
$decimalPlaces = substr($result[1], self::ZERO_DECIMAL_PLACE, $scale);
50+
$decimalPlaces = isset($result[1]) ? substr($result[1], self::ZERO_DECIMAL_PLACE, $scale) : '';
51+
$decimalPlaces = str_pad($decimalPlaces, $scale, '0');
5152

5253
$template = '%s.%s';
5354
$value = sprintf($template, $decimal, $decimalPlaces);

tests/BigDecimalTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public function testFromString(string $value): void
1818
/** @Then the created object should be an instance of both BigNumber and BigDecimal */
1919
self::assertInstanceOf(BigNumber::class, $actual);
2020
self::assertInstanceOf(BigDecimal::class, $actual);
21+
22+
/** @And the scale and value should be correctly initialized */
23+
self::assertSame($value, $actual->toString());
2124
}
2225

2326
#[DataProvider('dataProviderForFromFloat')]
@@ -29,6 +32,9 @@ public function testFromFloat(float $value): void
2932
/** @Then the created object should be an instance of both BigNumber and BigDecimal */
3033
self::assertInstanceOf(BigNumber::class, $actual);
3134
self::assertInstanceOf(BigDecimal::class, $actual);
35+
36+
/** @And the scale and value should be correctly initialized */
37+
self::assertSame($value, $actual->toFloat());
3238
}
3339

3440
public static function dataProviderForFromString(): array

tests/BigNumberTest.php

+44-8
Original file line numberDiff line numberDiff line change
@@ -411,29 +411,65 @@ public static function providerForTestWithRounding(): array
411411
public static function providerForTestWithScale(): array
412412
{
413413
return [
414-
'Scaling zero' => [
414+
'Scaling zero' => [
415415
'value' => 0,
416-
'scale' => 0,
417-
'withScale' => 0,
418-
'expected' => ['float' => 0, 'string' => '0']
416+
'scale' => 2,
417+
'withScale' => 2,
418+
'expected' => ['float' => 0.00, 'string' => '0.00']
419419
],
420-
'Scaling with decimal' => [
420+
'Scaling zero with one decimal' => [
421421
'value' => 0.0,
422+
'scale' => 2,
423+
'withScale' => 2,
424+
'expected' => ['float' => 0.00, 'string' => '0.00']
425+
],
426+
'Scaling zero with two decimals' => [
427+
'value' => 0.00,
428+
'scale' => 3,
429+
'withScale' => 3,
430+
'expected' => ['float' => 0.000, 'string' => '0.000']
431+
],
432+
'Scaling zero with three decimals' => [
433+
'value' => 0.000,
422434
'scale' => 1,
423435
'withScale' => 1,
424-
'expected' => ['float' => 0.0, 'string' => '0']
436+
'expected' => ['float' => 0.0, 'string' => '0.0']
437+
],
438+
'Scaling with integer' => [
439+
'value' => 5,
440+
'scale' => 2,
441+
'withScale' => 2,
442+
'expected' => ['float' => 5.00, 'string' => '5.00']
425443
],
426-
'Scaling large negative number' => [
444+
'Scaling with decimal and reducing precision' => [
445+
'value' => 123.4567,
446+
'scale' => 2,
447+
'withScale' => 2,
448+
'expected' => ['float' => 123.45, 'string' => '123.45']
449+
],
450+
'Scaling large negative number' => [
427451
'value' => -553.99999,
428452
'scale' => 5,
429453
'withScale' => 1,
430454
'expected' => ['float' => -553.9, 'string' => '-553.9']
431455
],
432-
'Scaling with precision reduction' => [
456+
'Scaling with precision reduction' => [
433457
'value' => 10.5555,
434458
'scale' => 4,
435459
'withScale' => 3,
436460
'expected' => ['float' => 10.555, 'string' => '10.555']
461+
],
462+
'Scaling large positive number' => [
463+
'value' => 999999.999,
464+
'scale' => 2,
465+
'withScale' => 2,
466+
'expected' => ['float' => 999999.99, 'string' => '999999.99']
467+
],
468+
'Scaling with small negative number' => [
469+
'value' => -0.12345,
470+
'scale' => 4,
471+
'withScale' => 4,
472+
'expected' => ['float' => -0.1234, 'string' => '-0.1234']
437473
]
438474
];
439475
}

tests/PositiveBigDecimalTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public function testFromFloat(): void
2222
/** @Then the created object should be an instance of both BigNumber and PositiveBigDecimal */
2323
self::assertInstanceOf(BigNumber::class, $actual);
2424
self::assertInstanceOf(PositiveBigDecimal::class, $actual);
25+
26+
/** @And the scale and value should be correctly initialized */
27+
self::assertSame($value, $actual->toFloat());
28+
self::assertNull($actual->getScale());
2529
}
2630

2731
public function testFromString(): void
@@ -35,6 +39,10 @@ public function testFromString(): void
3539
/** @Then the created object should be an instance of both BigNumber and PositiveBigDecimal */
3640
self::assertInstanceOf(BigNumber::class, $actual);
3741
self::assertInstanceOf(PositiveBigDecimal::class, $actual);
42+
43+
/** @And the scale and value should be correctly initialized */
44+
self::assertSame($value, $actual->toString());
45+
self::assertNull($actual->getScale());
3846
}
3947

4048
#[DataProvider('dataProviderForTestNonPositiveValue')]

0 commit comments

Comments
 (0)