{"record":{"id":"03408783a3940169","repo":"PHPOffice/PhpSpreadsheet","slug":"num-034087","errorCode":null,"errorMessage":"#NUM!","messagePattern":"#NUM!","errorType":"exception","errorClass":"PhpSpreadsheet\\Calculation\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Calculation/Engineering/ConvertBase.php","lineNumber":41,"sourceCode":"\n        if (is_numeric($value)) {\n            if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC) {\n                $value = floor((float) $value);\n            }\n        }\n\n        return strtoupper(StringHelper::convertToString($value));\n    }\n\n    protected static function validatePlaces(mixed $places = null): ?int\n    {\n        if ($places === null) {\n            return $places;\n        }\n\n        if (is_numeric($places)) {\n            if ($places < 0 || $places > 10) {\n                throw new Exception(ExcelError::NAN());\n            }\n\n            return (int) $places;\n        }\n\n        throw new Exception(ExcelError::VALUE());\n    }\n\n    /**\n     * Formats a number base string value with leading zeroes.\n     *\n     * @param string $value The \"number\" to pad\n     * @param ?int $places The length that we want to pad this value\n     *\n     * @return string The padded \"number\"\n     */\n    protected static function nbrConversionFormat(string $value, ?int $places): string\n    {","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Calculation/Engineering/ConvertBase.php#L23-L59","documentation":"#NUM! from the places (padding width) validator shared by all base-conversion functions that take a places argument (BIN2HEX, BIN2OCT, DEC2BIN, DEC2HEX, DEC2OCT, HEX2BIN, HEX2OCT, OCT2BIN, OCT2HEX). ConvertBase::validatePlaces() rejects numeric places below 0 or above 10 with Calculation\\Exception('#NUM!'), matching Excel's documented limit.","triggerScenarios":"=BIN2HEX(\"1010\", 11), =DEC2BIN(5, -1); ConvertHex::toBinary('1F', 12) from PHP; padding widths sourced from config that assume more than 10 output characters are possible (outputs are capped at 10 digits anyway).","commonSituations":"Wanting a fixed-width 16-character hex/binary output and passing 16; passing a negative 'unlimited' sentinel; computed widths from string length of the input plus padding.","solutions":["Keep places in the range 0..10 (it is truncated to int, so 3.9 becomes 3).","For wider output, pad the returned string yourself with str_pad() after the conversion.","Omit places (pass null) to get the minimum number of characters.","Note nbrConversionFormat() additionally returns #NUM! when the result needs more digits than places allows."],"exampleFix":"// before\n$hex = ConvertBinary::toHex('10101010', 16); // '#NUM!' - places > 10\n\n// after\n$hex = str_pad(ConvertBinary::toHex('10101010'), 16, '0', STR_PAD_LEFT);","handlingStrategy":"validation","validationCode":"$places = ($places === null) ? null : (int) $places;\nif ($places !== null && ($places < 0 || $places > 10)) {\n    throw new \\InvalidArgumentException('places must be between 0 and 10');\n}\n$result = ConvertDecimal::toBinary($value, $places);","typeGuard":"/** places must be null or an integer 0..10. */\nfunction isValidPlaces(mixed $places): bool\n{\n    return $places === null || (is_numeric($places) && $places >= 0 && $places <= 10);\n}","tryCatchPattern":null,"preventionTips":["Outputs are capped at 10 characters - pad wider results yourself with str_pad().","Omit places (null) when minimum width is acceptable.","places is truncated, not rounded: 10.9 becomes 10, 10.99 is fine but 11 is not."],"tags":["phpspreadsheet","excel-formula","base-conversion","padding","argument-range"],"backgroundTag":"excel-num-error","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}