Skip to content

Commit

Permalink
Release/3.4.1 (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavofreze authored Nov 7, 2024
1 parent b4f8cb5 commit b79c4ff
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 14 deletions.
1 change: 0 additions & 1 deletion infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"BCMath": false,
"CastInt": false,
"Ternary": false,
"MethodCallRemoval": false,
"ProtectedVisibility": false
},
"phpUnit": {
Expand Down
11 changes: 6 additions & 5 deletions src/Internal/Scale.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ public function scaleOf(string $value): Scale

public function numberWithScale(Number $number, int $scale): Number
{
$result = explode('.', $number->value);

if (count($result) <= 1) {
return Number::from(value: $result[0]);
if ($number->isZero()) {
$formattedValue = number_format(0, $scale, '.', '');
return Number::from(value: $formattedValue);
}

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

$template = '%s.%s';
$value = sprintf($template, $decimal, $decimalPlaces);
Expand Down
6 changes: 6 additions & 0 deletions tests/BigDecimalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public function testFromString(string $value): void
/** @Then the created object should be an instance of both BigNumber and BigDecimal */
self::assertInstanceOf(BigNumber::class, $actual);
self::assertInstanceOf(BigDecimal::class, $actual);

/** @And the scale and value should be correctly initialized */
self::assertSame($value, $actual->toString());
}

#[DataProvider('dataProviderForFromFloat')]
Expand All @@ -29,6 +32,9 @@ public function testFromFloat(float $value): void
/** @Then the created object should be an instance of both BigNumber and BigDecimal */
self::assertInstanceOf(BigNumber::class, $actual);
self::assertInstanceOf(BigDecimal::class, $actual);

/** @And the scale and value should be correctly initialized */
self::assertSame($value, $actual->toFloat());
}

public static function dataProviderForFromString(): array
Expand Down
52 changes: 44 additions & 8 deletions tests/BigNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,29 +411,65 @@ public static function providerForTestWithRounding(): array
public static function providerForTestWithScale(): array
{
return [
'Scaling zero' => [
'Scaling zero' => [
'value' => 0,
'scale' => 0,
'withScale' => 0,
'expected' => ['float' => 0, 'string' => '0']
'scale' => 2,
'withScale' => 2,
'expected' => ['float' => 0.00, 'string' => '0.00']
],
'Scaling with decimal' => [
'Scaling zero with one decimal' => [
'value' => 0.0,
'scale' => 2,
'withScale' => 2,
'expected' => ['float' => 0.00, 'string' => '0.00']
],
'Scaling zero with two decimals' => [
'value' => 0.00,
'scale' => 3,
'withScale' => 3,
'expected' => ['float' => 0.000, 'string' => '0.000']
],
'Scaling zero with three decimals' => [
'value' => 0.000,
'scale' => 1,
'withScale' => 1,
'expected' => ['float' => 0.0, 'string' => '0']
'expected' => ['float' => 0.0, 'string' => '0.0']
],
'Scaling with integer' => [
'value' => 5,
'scale' => 2,
'withScale' => 2,
'expected' => ['float' => 5.00, 'string' => '5.00']
],
'Scaling large negative number' => [
'Scaling with decimal and reducing precision' => [
'value' => 123.4567,
'scale' => 2,
'withScale' => 2,
'expected' => ['float' => 123.45, 'string' => '123.45']
],
'Scaling large negative number' => [
'value' => -553.99999,
'scale' => 5,
'withScale' => 1,
'expected' => ['float' => -553.9, 'string' => '-553.9']
],
'Scaling with precision reduction' => [
'Scaling with precision reduction' => [
'value' => 10.5555,
'scale' => 4,
'withScale' => 3,
'expected' => ['float' => 10.555, 'string' => '10.555']
],
'Scaling large positive number' => [
'value' => 999999.999,
'scale' => 2,
'withScale' => 2,
'expected' => ['float' => 999999.99, 'string' => '999999.99']
],
'Scaling with small negative number' => [
'value' => -0.12345,
'scale' => 4,
'withScale' => 4,
'expected' => ['float' => -0.1234, 'string' => '-0.1234']
]
];
}
Expand Down
8 changes: 8 additions & 0 deletions tests/PositiveBigDecimalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public function testFromFloat(): void
/** @Then the created object should be an instance of both BigNumber and PositiveBigDecimal */
self::assertInstanceOf(BigNumber::class, $actual);
self::assertInstanceOf(PositiveBigDecimal::class, $actual);

/** @And the scale and value should be correctly initialized */
self::assertSame($value, $actual->toFloat());
self::assertNull($actual->getScale());
}

public function testFromString(): void
Expand All @@ -35,6 +39,10 @@ public function testFromString(): void
/** @Then the created object should be an instance of both BigNumber and PositiveBigDecimal */
self::assertInstanceOf(BigNumber::class, $actual);
self::assertInstanceOf(PositiveBigDecimal::class, $actual);

/** @And the scale and value should be correctly initialized */
self::assertSame($value, $actual->toString());
self::assertNull($actual->getScale());
}

#[DataProvider('dataProviderForTestNonPositiveValue')]
Expand Down

0 comments on commit b79c4ff

Please sign in to comment.