Skip to content

Commit acf20f0

Browse files
authored
Merge pull request #4389 from oleibman/issue4383
BIN2DEC, OCT2DEC, HEX2DEC Return Numbers Rather than Strings
2 parents 93069fd + a67a981 commit acf20f0

File tree

13 files changed

+146
-134
lines changed

13 files changed

+146
-134
lines changed

CHANGELOG.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
1717

1818
### Changed
1919

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

2222
### Moved
2323

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

3030
### Fixed
3131

32-
- Nothing yet.
32+
- 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)
3333

3434
## 2025-03-02 - 4.1.0
3535

3636
### Added
3737

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

4141
### Changed
4242

src/PhpSpreadsheet/Calculation/Engineering/ConvertBinary.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ConvertBinary extends ConvertBase
2323
* 10 characters (10 bits), BIN2DEC returns the #NUM! error value.
2424
* Or can be an array of values
2525
*
26-
* @return array|string Result, or an error
26+
* @return array|float|int|string Result, or an error
2727
* If an array of numbers is passed as an argument, then the returned result will also be an array
2828
* with the same dimensions
2929
*/
@@ -44,10 +44,10 @@ public static function toDecimal($value)
4444
// Two's Complement
4545
$value = substr($value, -9);
4646

47-
return '-' . (512 - bindec($value));
47+
return -(512 - bindec($value));
4848
}
4949

50-
return (string) bindec($value);
50+
return bindec($value);
5151
}
5252

5353
/**

src/PhpSpreadsheet/Calculation/Engineering/ConvertHex.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public static function toBinary($value, $places = null): array|string
7474
* #NUM! error value.
7575
* Or can be an array of values
7676
*
77-
* @return array|string Result, or an error
77+
* @return array|float|int|string Result, or an error
7878
* If an array of numbers is passed as an argument, then the returned result will also be an array
7979
* with the same dimensions
8080
*/
@@ -104,10 +104,10 @@ public static function toDecimal($value)
104104
$binX[$i] = ($binX[$i] == '1' ? '0' : '1');
105105
}
106106

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

110-
return (string) bindec($binX);
110+
return bindec($binX);
111111
}
112112

113113
/**

src/PhpSpreadsheet/Calculation/Engineering/ConvertOctal.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static function toBinary($value, $places = null): array|string
7878
* #NUM! error value.
7979
* Or can be an array of values
8080
*
81-
* @return array|string Result, or an error
81+
* @return array|float|int|string Result, or an error
8282
* If an array of numbers is passed as an argument, then the returned result will also be an array
8383
* with the same dimensions
8484
*/
@@ -104,10 +104,10 @@ public static function toDecimal($value)
104104
$binX[$i] = ($binX[$i] == '1' ? '0' : '1');
105105
}
106106

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

110-
return (string) bindec($binX);
110+
return bindec($binX);
111111
}
112112

113113
/**

tests/PhpSpreadsheetTests/Calculation/Functions/Engineering/Bin2DecTest.php

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
1212
use PhpOffice\PhpSpreadsheet\Spreadsheet;
1313
use PhpOffice\PhpSpreadsheetTests\Calculation\Functions\FormulaArguments;
14+
use PHPUnit\Framework\Attributes\DataProvider;
1415
use PHPUnit\Framework\TestCase;
1516

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

30-
#[\PHPUnit\Framework\Attributes\DataProvider('providerBIN2DEC')]
31-
public function testDirectCallToBIN2DEC(string $expectedResult, bool|int|string $arg1): void
31+
#[DataProvider('providerBIN2DEC')]
32+
public function testDirectCallToBIN2DEC(float|int|string $expectedResult, bool|int|string $arg1): void
3233
{
3334
$result = ConvertBinary::toDecimal($arg1);
3435
self::assertSame($expectedResult, $result);
3536
}
3637

37-
private function trimIfQuoted(string $value): string
38-
{
39-
return trim($value, '"');
40-
}
41-
42-
#[\PHPUnit\Framework\Attributes\DataProvider('providerBIN2DEC')]
38+
#[DataProvider('providerBIN2DEC')]
4339
public function testBIN2DECAsFormula(mixed $expectedResult, mixed ...$args): void
4440
{
4541
$arguments = new FormulaArguments(...$args);
@@ -48,11 +44,11 @@ public function testBIN2DECAsFormula(mixed $expectedResult, mixed ...$args): voi
4844
$formula = "=BIN2DEC({$arguments})";
4945

5046
/** @var float|int|string */
51-
$result = $calculation->_calculateFormulaValue($formula);
52-
self::assertSame($expectedResult, $this->trimIfQuoted((string) $result));
47+
$result = $calculation->calculateFormula($formula);
48+
self::assertSame($expectedResult, $result);
5349
}
5450

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

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

104-
#[\PHPUnit\Framework\Attributes\DataProvider('providerBIN2DECOds')]
105-
public function testBIN2DECOds(string $expectedResult, bool $arg1): void
100+
#[DataProvider('providerBIN2DECOds')]
101+
public function testBIN2DECOds(float|int|string $expectedResult, bool $arg1): void
106102
{
107-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);
103+
Functions::setCompatibilityMode(
104+
Functions::COMPATIBILITY_OPENOFFICE
105+
);
108106

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

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

128-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);
128+
Functions::setCompatibilityMode(
129+
Functions::COMPATIBILITY_OPENOFFICE
130+
);
129131
/** @var float|int|string */
130-
$result = $calculation->_calculateFormulaValue($formula);
131-
self::assertSame(ExcelError::NAN(), $this->trimIfQuoted((string) $result), 'OpenOffice');
132+
$result = $calculation->calculateFormula($formula);
133+
self::assertSame(ExcelError::NAN(), $result, 'OpenOffice');
132134

133-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
135+
Functions::setCompatibilityMode(
136+
Functions::COMPATIBILITY_EXCEL
137+
);
134138
/** @var float|int|string */
135-
$result = $calculation->_calculateFormulaValue($formula);
136-
self::assertSame(ExcelError::NAN(), $this->trimIfQuoted((string) $result), 'Excel');
139+
$result = $calculation->calculateFormula($formula);
140+
self::assertSame(ExcelError::NAN(), $result, 'Excel');
137141
}
138142

139-
#[\PHPUnit\Framework\Attributes\DataProvider('providerBin2DecArray')]
143+
#[DataProvider('providerBin2DecArray')]
140144
public function testBin2DecArray(array $expectedResult, string $value): void
141145
{
142146
$calculation = Calculation::getInstance();
143147

144148
$formula = "=BIN2DEC({$value})";
145-
$result = $calculation->_calculateFormulaValue($formula);
149+
$result = $calculation->calculateFormula($formula);
146150
self::assertEquals($expectedResult, $result);
147151
}
148152

tests/PhpSpreadsheetTests/Calculation/Functions/Engineering/Hex2DecTest.php

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
1212
use PhpOffice\PhpSpreadsheet\Spreadsheet;
1313
use PhpOffice\PhpSpreadsheetTests\Calculation\Functions\FormulaArguments;
14+
use PHPUnit\Framework\Attributes\DataProvider;
1415
use PHPUnit\Framework\TestCase;
1516

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

30-
#[\PHPUnit\Framework\Attributes\DataProvider('providerHEX2DEC')]
31+
#[DataProvider('providerHEX2DEC')]
3132
public function testDirectCallToHEX2DEC(mixed $expectedResult, bool|float|int|string $value): void
3233
{
3334
$result = ConvertHex::toDecimal($value);
3435
self::assertSame($expectedResult, $result);
3536
}
3637

37-
private function trimIfQuoted(string $value): string
38-
{
39-
return trim($value, '"');
40-
}
41-
42-
#[\PHPUnit\Framework\Attributes\DataProvider('providerHEX2DEC')]
38+
#[DataProvider('providerHEX2DEC')]
4339
public function testHEX2DECAsFormula(mixed $expectedResult, mixed ...$args): void
4440
{
4541
$arguments = new FormulaArguments(...$args);
@@ -48,11 +44,11 @@ public function testHEX2DECAsFormula(mixed $expectedResult, mixed ...$args): voi
4844
$formula = "=HEX2DEC({$arguments})";
4945

5046
/** @var float|int|string */
51-
$result = $calculation->_calculateFormulaValue($formula);
52-
self::assertSame($expectedResult, $this->trimIfQuoted((string) $result));
47+
$result = $calculation->calculateFormula($formula);
48+
self::assertSame($expectedResult, $result);
5349
}
5450

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

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

104-
#[\PHPUnit\Framework\Attributes\DataProvider('providerHEX2DECOds')]
100+
#[DataProvider('providerHEX2DECOds')]
105101
public function testHEX2DECOds(mixed $expectedResult, bool|float|int|string $value): void
106102
{
107-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);
103+
Functions::setCompatibilityMode(
104+
Functions::COMPATIBILITY_OPENOFFICE
105+
);
108106

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

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

128-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);
128+
Functions::setCompatibilityMode(
129+
Functions::COMPATIBILITY_OPENOFFICE
130+
);
129131
/** @var float|int|string */
130-
$result = $calculation->_calculateFormulaValue($formula);
131-
self::assertSame(ExcelError::NAN(), $this->trimIfQuoted((string) $result), 'OpenOffice');
132+
$result = $calculation->calculateFormula($formula);
133+
self::assertSame(ExcelError::NAN(), $result, 'OpenOffice');
132134

133-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
135+
Functions::setCompatibilityMode(
136+
Functions::COMPATIBILITY_EXCEL
137+
);
134138
/** @var float|int|string */
135-
$result = $calculation->_calculateFormulaValue($formula);
136-
self::assertSame(ExcelError::NAN(), $this->trimIfQuoted((string) $result), 'Excel');
139+
$result = $calculation->calculateFormula($formula);
140+
self::assertSame(ExcelError::NAN(), $result, 'Excel');
137141
}
138142

139-
#[\PHPUnit\Framework\Attributes\DataProvider('providerHex2DecArray')]
143+
#[DataProvider('providerHex2DecArray')]
140144
public function testHex2DecArray(array $expectedResult, string $value): void
141145
{
142146
$calculation = Calculation::getInstance();
143147

144148
$formula = "=HEX2DEC({$value})";
145-
$result = $calculation->_calculateFormulaValue($formula);
149+
$result = $calculation->calculateFormula($formula);
146150
self::assertEquals($expectedResult, $result);
147151
}
148152

0 commit comments

Comments
 (0)