{"record":{"id":"09bcfe57d58ecebc","repo":"PHPOffice/PhpSpreadsheet","slug":"invalid-numeric-value-for-datatype-numeric","errorCode":null,"errorMessage":"Invalid numeric value for datatype Numeric","messagePattern":"Invalid numeric value for datatype Numeric","errorType":"exception","errorClass":"SpreadsheetException","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Cell/Cell.php","lineNumber":330,"sourceCode":"                    $quotePrefix = true;\n                }\n                // no break\n            case DataType::TYPE_INLINE:\n                // Rich text\n                $value2 = StringHelper::convertToString($value, true);\n                // Cells?->Worksheet?->Spreadsheet\n                $binder = $this->parent?->getParent()?->getParent()?->getValueBinder();\n                $preserveCr = false;\n                if ($binder !== null && method_exists($binder, 'getPreserveCr')) {\n                    /** @var bool */\n                    $preserveCr = $binder->getPreserveCr();\n                }\n                $this->value = DataType::checkString(($value instanceof RichText) ? $value : $value2, $preserveCr);\n\n                break;\n            case DataType::TYPE_NUMERIC:\n                if ($value !== null && !is_bool($value) && !is_numeric($value)) {\n                    throw new SpreadsheetException('Invalid numeric value for datatype Numeric');\n                }\n                $this->value = 0 + $value;\n\n                break;\n            case DataType::TYPE_FORMULA:\n                $this->value = StringHelper::convertToString($value, true);\n\n                break;\n            case DataType::TYPE_BOOL:\n                $this->value = (bool) $value;\n\n                break;\n            case DataType::TYPE_ISO_DATE:\n                $this->value = SharedDate::convertIsoDate($value);\n                $dataType = DataType::TYPE_NUMERIC;\n\n                break;\n            case DataType::TYPE_DRAWING_IN_CELL:","sourceCodeStart":312,"sourceCodeEnd":348,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Cell/Cell.php#L312-L348","documentation":"setValueExplicit($value, DataType::TYPE_NUMERIC) (type 'n') only accepts null, booleans, and numeric values; the guard rejects anything else before the 0 + $value coercion runs. Strings like 'abc', '', or locale-formatted numbers such as '1.234,56' are not is_numeric() and therefore throw. The check is strict by design so an explicitly numeric cell never holds non-numeric data.","triggerScenarios":"setValueExplicit($rawInput, DataType::TYPE_NUMERIC) with unvalidated user/CSV input; locale-formatted decimal strings; empty strings from optional fields; objects without a numeric value.","commonSituations":"High-throughput writers switched from setValue() (auto-detecting) to setValueExplicit() for performance, then fed raw strings; importing spreadsheets/CSVs where the source column mixes numbers and text.","solutions":["Validate first: if (is_numeric($value)) then cast with (float) $value (or (int)), else write as TYPE_STRING or handle the bad input","Use setValue() when the type is not guaranteed - the default binder picks the right datatype","Normalize locale separators and trim whitespace before the call"],"exampleFix":"// before\n$cell->setValueExplicit($rawInput, DataType::TYPE_NUMERIC); // 'abc' or '1.234,56' throws\n\n// after\n$value = str_replace(['.', ','], ['', '.'], trim($rawInput));\n$cell->setValueExplicit(is_numeric($value) ? (float) $value : 0.0, DataType::TYPE_NUMERIC);","handlingStrategy":"validation","validationCode":"$value = trim($raw);\nif (!is_numeric($value) && $value !== null && !is_bool($value)) {\n    $cell->setValueExplicit($value, DataType::TYPE_STRING); // keep as text\n} else {\n    $cell->setValueExplicit($value === null ? 0.0 : (float) $value, DataType::TYPE_NUMERIC);\n}","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Run is_numeric() on raw input before forcing TYPE_NUMERIC","Prefer setValue() for untrusted data - the default binder chooses the datatype","Normalize locale decimal separators (comma/dot) during CSV import"],"tags":["phpspreadsheet","datatype","numeric","type-mismatch","cell-value"],"backgroundTag":"invalid-numeric-value","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}