{"id":"abfe78749c398e91","repo":"laravel/framework","slug":"cache-value-for-key-s-must-be-a-float-s-given","errorCode":null,"errorMessage":"Cache value for key [%s] must be a float, %s given.","messagePattern":"Cache value for key \\[(.+?)\\] must be a float, (.+?) given\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Cache/Repository.php","lineNumber":307,"sourceCode":"     * @param  (\\Closure():(float|null))|float|null  $default\n     *\n     * @throws \\InvalidArgumentException\n     */\n    public function float($key, $default = null): float\n    {\n        $key = enum_value($key);\n\n        $value = $this->get($key, $default);\n\n        if (is_float($value)) {\n            return $value;\n        }\n\n        if (filter_var($value, FILTER_VALIDATE_FLOAT) !== false) {\n            return (float) $value;\n        }\n\n        throw new InvalidArgumentException(\n            sprintf('Cache value for key [%s] must be a float, %s given.', $key, gettype($value))\n        );\n    }\n\n    /**\n     * Retrieve a boolean item from the cache.\n     *\n     * @param  \\UnitEnum|string  $key\n     * @param  (\\Closure():(bool|null))|bool|null  $default\n     *\n     * @throws \\InvalidArgumentException\n     */\n    public function boolean($key, $default = null): bool\n    {\n        $key = enum_value($key);\n\n        $value = $this->get($key, $default);\n","sourceCodeStart":289,"sourceCodeEnd":325,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Cache/Repository.php#L289-L325","documentation":"Thrown by Cache\\Repository::float() when the stored value is not a float and cannot be coerced via filter_var(FILTER_VALIDATE_FLOAT). Genuine ints are NOT auto-promoted by is_float() and filter_var on an int returns the int which is !== false, so ints are accepted; the error fires for arrays, bools, non-numeric strings, or null.","triggerScenarios":"Calling Cache::float('rate') when the stored value is an array, bool, the string 'n/a', or null because the key is absent and no float default was supplied.","commonSituations":"Caching a rate as a formatted string '3.14%' (the % breaks FILTER_VALIDATE_FLOAT) and reading with float(); missing key without default; storing an array of metrics under a scalar key.","solutions":["Pass a float default: Cache::float('rate', 0.0).","Sanitize the writer: store the raw numeric value, Cache::put('rate', (float) $value).","Strip non-numeric characters before storing if the source is a formatted string.","Cache::forget('rate') to clear a malformed entry."],"exampleFix":"// before\n$rate = Cache::float('conversion.rate');\n\n// after\n$rate = Cache::float('conversion.rate', 0.0);\nCache::put('conversion.rate', (float) $raw, $ttl);","handlingStrategy":"type-guard","validationCode":"$value = Cache::get('rate');\nif (! is_float($value) && filter_var($value, FILTER_VALIDATE_FLOAT) === false) {\n    $value = 0.0;\n}","typeGuard":"function isCachedFloat(string $key): bool {\n    $v = Cache::get($key);\n    return is_float($v) || filter_var($v, FILTER_VALIDATE_FLOAT) !== false;\n}","tryCatchPattern":"try {\n    $rate = Cache::float('rate', 0.0);\n} catch (\\InvalidArgumentException $e) {\n    $rate = 0.0;\n}","preventionTips":["Store raw numeric values, not formatted strings like '3.14%'.","Pass a float default: Cache::float('rate', 0.0).","Validate the source before caching if it can be 'N/A' or empty."],"tags":["cache","type-mismatch","laravel"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}