Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BIN2DEC, OCT2DEC, HEX2DEC Return Numbers Rather than Strings #4389

Merged
merged 1 commit into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).

### Changed

- Nothing yet.
- Phpstan Version 2. [PR #4384](https://github.com/PHPOffice/PhpSpreadsheet/pull/4384)

### Moved

Expand All @@ -29,14 +29,14 @@ and this project adheres to [Semantic Versioning](https://semver.org).

### Fixed

- Nothing yet.
- BIN2DEC, OCT2DEC, and HEX2DEC return numbers rather than strings. [Issue #4383](https://github.com/PHPOffice/PhpSpreadsheet/issues/4383) [PR #4389](https://github.com/PHPOffice/PhpSpreadsheet/pull/4389)

## 2025-03-02 - 4.1.0

### Added

- Support Justify Last Line. [Issue #4374](https://github.com/PHPOffice/PhpSpreadsheet/issues/4374) [PR #4373](https://github.com/PHPOffice/PhpSpreadsheet/pull/4373)
- Allow Spreadsheet clone. [PR #437-](https://github.com/PHPOffice/PhpSpreadsheet/pull/4370)
- Allow Spreadsheet clone. [PR #4370](https://github.com/PHPOffice/PhpSpreadsheet/pull/4370)

### Changed

Expand Down
6 changes: 3 additions & 3 deletions src/PhpSpreadsheet/Calculation/Engineering/ConvertBinary.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ConvertBinary extends ConvertBase
* 10 characters (10 bits), BIN2DEC returns the #NUM! error value.
* Or can be an array of values
*
* @return array|string Result, or an error
* @return array|float|int|string Result, or an error
* If an array of numbers is passed as an argument, then the returned result will also be an array
* with the same dimensions
*/
Expand All @@ -44,10 +44,10 @@ public static function toDecimal($value)
// Two's Complement
$value = substr($value, -9);

return '-' . (512 - bindec($value));
return -(512 - bindec($value));
}

return (string) bindec($value);
return bindec($value);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/PhpSpreadsheet/Calculation/Engineering/ConvertHex.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static function toBinary($value, $places = null): array|string
* #NUM! error value.
* Or can be an array of values
*
* @return array|string Result, or an error
* @return array|float|int|string Result, or an error
* If an array of numbers is passed as an argument, then the returned result will also be an array
* with the same dimensions
*/
Expand Down Expand Up @@ -104,10 +104,10 @@ public static function toDecimal($value)
$binX[$i] = ($binX[$i] == '1' ? '0' : '1');
}

return (string) ((bindec($binX) + 1) * -1);
return (bindec($binX) + 1) * -1;
}

return (string) bindec($binX);
return bindec($binX);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/PhpSpreadsheet/Calculation/Engineering/ConvertOctal.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static function toBinary($value, $places = null): array|string
* #NUM! error value.
* Or can be an array of values
*
* @return array|string Result, or an error
* @return array|float|int|string Result, or an error
* If an array of numbers is passed as an argument, then the returned result will also be an array
* with the same dimensions
*/
Expand All @@ -104,10 +104,10 @@ public static function toDecimal($value)
$binX[$i] = ($binX[$i] == '1' ? '0' : '1');
}

return (string) ((bindec($binX) + 1) * -1);
return (bindec($binX) + 1) * -1;
}

return (string) bindec($binX);
return bindec($binX);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheetTests\Calculation\Functions\FormulaArguments;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class Bin2DecTest extends TestCase
Expand All @@ -27,19 +28,14 @@ protected function tearDown(): void
Functions::setCompatibilityMode($this->compatibilityMode);
}

#[\PHPUnit\Framework\Attributes\DataProvider('providerBIN2DEC')]
public function testDirectCallToBIN2DEC(string $expectedResult, bool|int|string $arg1): void
#[DataProvider('providerBIN2DEC')]
public function testDirectCallToBIN2DEC(float|int|string $expectedResult, bool|int|string $arg1): void
{
$result = ConvertBinary::toDecimal($arg1);
self::assertSame($expectedResult, $result);
}

private function trimIfQuoted(string $value): string
{
return trim($value, '"');
}

#[\PHPUnit\Framework\Attributes\DataProvider('providerBIN2DEC')]
#[DataProvider('providerBIN2DEC')]
public function testBIN2DECAsFormula(mixed $expectedResult, mixed ...$args): void
{
$arguments = new FormulaArguments(...$args);
Expand All @@ -48,11 +44,11 @@ public function testBIN2DECAsFormula(mixed $expectedResult, mixed ...$args): voi
$formula = "=BIN2DEC({$arguments})";

/** @var float|int|string */
$result = $calculation->_calculateFormulaValue($formula);
self::assertSame($expectedResult, $this->trimIfQuoted((string) $result));
$result = $calculation->calculateFormula($formula);
self::assertSame($expectedResult, $result);
}

#[\PHPUnit\Framework\Attributes\DataProvider('providerBIN2DEC')]
#[DataProvider('providerBIN2DEC')]
public function testBIN2DECInWorksheet(mixed $expectedResult, mixed ...$args): void
{
$arguments = new FormulaArguments(...$args);
Expand All @@ -75,7 +71,7 @@ public static function providerBIN2DEC(): array
return require 'tests/data/Calculation/Engineering/BIN2DEC.php';
}

#[\PHPUnit\Framework\Attributes\DataProvider('providerUnhappyBIN2DEC')]
#[DataProvider('providerUnhappyBIN2DEC')]
public function testBIN2DECUnhappyPath(string $expectedException, mixed ...$args): void
{
$arguments = new FormulaArguments(...$args);
Expand All @@ -101,10 +97,12 @@ public static function providerUnhappyBIN2DEC(): array
];
}

#[\PHPUnit\Framework\Attributes\DataProvider('providerBIN2DECOds')]
public function testBIN2DECOds(string $expectedResult, bool $arg1): void
#[DataProvider('providerBIN2DECOds')]
public function testBIN2DECOds(float|int|string $expectedResult, bool $arg1): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);
Functions::setCompatibilityMode(
Functions::COMPATIBILITY_OPENOFFICE
);

$result = ConvertBinary::toDecimal($arg1);
self::assertSame($expectedResult, $result);
Expand All @@ -120,29 +118,35 @@ public function testBIN2DECFractional(): void
$calculation = Calculation::getInstance();
$formula = '=BIN2DEC(101.1)';

Functions::setCompatibilityMode(Functions::COMPATIBILITY_GNUMERIC);
Functions::setCompatibilityMode(
Functions::COMPATIBILITY_GNUMERIC
);
/** @var float|int|string */
$result = $calculation->_calculateFormulaValue($formula);
self::assertSame('5', $this->trimIfQuoted((string) $result), 'Gnumeric');
$result = $calculation->calculateFormula($formula);
self::assertSame(5, $result, 'Gnumeric');

Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);
Functions::setCompatibilityMode(
Functions::COMPATIBILITY_OPENOFFICE
);
/** @var float|int|string */
$result = $calculation->_calculateFormulaValue($formula);
self::assertSame(ExcelError::NAN(), $this->trimIfQuoted((string) $result), 'OpenOffice');
$result = $calculation->calculateFormula($formula);
self::assertSame(ExcelError::NAN(), $result, 'OpenOffice');

Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
Functions::setCompatibilityMode(
Functions::COMPATIBILITY_EXCEL
);
/** @var float|int|string */
$result = $calculation->_calculateFormulaValue($formula);
self::assertSame(ExcelError::NAN(), $this->trimIfQuoted((string) $result), 'Excel');
$result = $calculation->calculateFormula($formula);
self::assertSame(ExcelError::NAN(), $result, 'Excel');
}

#[\PHPUnit\Framework\Attributes\DataProvider('providerBin2DecArray')]
#[DataProvider('providerBin2DecArray')]
public function testBin2DecArray(array $expectedResult, string $value): void
{
$calculation = Calculation::getInstance();

$formula = "=BIN2DEC({$value})";
$result = $calculation->_calculateFormulaValue($formula);
$result = $calculation->calculateFormula($formula);
self::assertEquals($expectedResult, $result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheetTests\Calculation\Functions\FormulaArguments;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class Hex2DecTest extends TestCase
Expand All @@ -27,19 +28,14 @@ protected function tearDown(): void
Functions::setCompatibilityMode($this->compatibilityMode);
}

#[\PHPUnit\Framework\Attributes\DataProvider('providerHEX2DEC')]
#[DataProvider('providerHEX2DEC')]
public function testDirectCallToHEX2DEC(mixed $expectedResult, bool|float|int|string $value): void
{
$result = ConvertHex::toDecimal($value);
self::assertSame($expectedResult, $result);
}

private function trimIfQuoted(string $value): string
{
return trim($value, '"');
}

#[\PHPUnit\Framework\Attributes\DataProvider('providerHEX2DEC')]
#[DataProvider('providerHEX2DEC')]
public function testHEX2DECAsFormula(mixed $expectedResult, mixed ...$args): void
{
$arguments = new FormulaArguments(...$args);
Expand All @@ -48,11 +44,11 @@ public function testHEX2DECAsFormula(mixed $expectedResult, mixed ...$args): voi
$formula = "=HEX2DEC({$arguments})";

/** @var float|int|string */
$result = $calculation->_calculateFormulaValue($formula);
self::assertSame($expectedResult, $this->trimIfQuoted((string) $result));
$result = $calculation->calculateFormula($formula);
self::assertSame($expectedResult, $result);
}

#[\PHPUnit\Framework\Attributes\DataProvider('providerHEX2DEC')]
#[DataProvider('providerHEX2DEC')]
public function testHEX2DECInWorksheet(mixed $expectedResult, mixed ...$args): void
{
$arguments = new FormulaArguments(...$args);
Expand All @@ -75,7 +71,7 @@ public static function providerHEX2DEC(): array
return require 'tests/data/Calculation/Engineering/HEX2DEC.php';
}

#[\PHPUnit\Framework\Attributes\DataProvider('providerUnhappyHEX2DEC')]
#[DataProvider('providerUnhappyHEX2DEC')]
public function testHEX2DECUnhappyPath(string $expectedException, mixed ...$args): void
{
$arguments = new FormulaArguments(...$args);
Expand All @@ -101,10 +97,12 @@ public static function providerUnhappyHEX2DEC(): array
];
}

#[\PHPUnit\Framework\Attributes\DataProvider('providerHEX2DECOds')]
#[DataProvider('providerHEX2DECOds')]
public function testHEX2DECOds(mixed $expectedResult, bool|float|int|string $value): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);
Functions::setCompatibilityMode(
Functions::COMPATIBILITY_OPENOFFICE
);

$result = ConvertHex::toDecimal($value);
self::assertSame($expectedResult, $result);
Expand All @@ -120,29 +118,35 @@ public function testHEX2DECFrac(): void
$calculation = Calculation::getInstance();
$formula = '=HEX2DEC(10.1)';

Functions::setCompatibilityMode(Functions::COMPATIBILITY_GNUMERIC);
Functions::setCompatibilityMode(
Functions::COMPATIBILITY_GNUMERIC
);
/** @var float|int|string */
$result = $calculation->_calculateFormulaValue($formula);
self::assertSame('16', $this->trimIfQuoted((string) $result), 'Gnumeric');
$result = $calculation->calculateFormula($formula);
self::assertSame(16, $result, 'Gnumeric');

Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);
Functions::setCompatibilityMode(
Functions::COMPATIBILITY_OPENOFFICE
);
/** @var float|int|string */
$result = $calculation->_calculateFormulaValue($formula);
self::assertSame(ExcelError::NAN(), $this->trimIfQuoted((string) $result), 'OpenOffice');
$result = $calculation->calculateFormula($formula);
self::assertSame(ExcelError::NAN(), $result, 'OpenOffice');

Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
Functions::setCompatibilityMode(
Functions::COMPATIBILITY_EXCEL
);
/** @var float|int|string */
$result = $calculation->_calculateFormulaValue($formula);
self::assertSame(ExcelError::NAN(), $this->trimIfQuoted((string) $result), 'Excel');
$result = $calculation->calculateFormula($formula);
self::assertSame(ExcelError::NAN(), $result, 'Excel');
}

#[\PHPUnit\Framework\Attributes\DataProvider('providerHex2DecArray')]
#[DataProvider('providerHex2DecArray')]
public function testHex2DecArray(array $expectedResult, string $value): void
{
$calculation = Calculation::getInstance();

$formula = "=HEX2DEC({$value})";
$result = $calculation->_calculateFormulaValue($formula);
$result = $calculation->calculateFormula($formula);
self::assertEquals($expectedResult, $result);
}

Expand Down
Loading