{"id":"173f2e785cef5b1b","repo":"gohugoio/hugo","slug":"floor-operator-can-t-be-used-with-non-float-value","errorCode":null,"errorMessage":"Floor operator can't be used with non-float value","messagePattern":"Floor operator can't be used with non-float value","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/math/math.go","lineNumber":129,"sourceCode":"// Cos returns the cosine of the radian argument n.\nfunc (ns *Namespace) Cos(n any) (float64, error) {\n\taf, err := cast.ToFloat64E(n)\n\tif err != nil {\n\t\treturn 0, errors.New(\"requires a numeric argument\")\n\t}\n\treturn math.Cos(af), nil\n}\n\n// Div divides n1 by n2.\nfunc (ns *Namespace) Div(inputs ...any) (any, error) {\n\treturn ns.doArithmetic(inputs, '/')\n}\n\n// Floor returns the greatest integer value less than or equal to n.\nfunc (ns *Namespace) Floor(n any) (float64, error) {\n\txf, err := cast.ToFloat64E(n)\n\tif err != nil {\n\t\treturn 0, errors.New(\"Floor operator can't be used with non-float value\")\n\t}\n\n\treturn math.Floor(xf), nil\n}\n\n// Log returns the natural logarithm of the number n.\nfunc (ns *Namespace) Log(n any) (float64, error) {\n\taf, err := cast.ToFloat64E(n)\n\tif err != nil {\n\t\treturn 0, errors.New(\"Log operator can't be used with non integer or float value\")\n\t}\n\n\treturn math.Log(af), nil\n}\n\n// Max returns the greater of all numbers in inputs. Any slices in inputs are flattened.\nfunc (ns *Namespace) Max(inputs ...any) (maximum float64, err error) {\n\treturn ns.applyOpToScalarsOrSlices(\"Max\", math.Max, inputs...)","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/tpl/math/math.go#L111-L147","documentation":"math.Floor mirrors Ceil: the argument is cast to float64 and this error is returned when cast.ToFloat64E fails. It indicates the template passed a value that cannot be interpreted as a number (nil, non-numeric string, map, slice).","triggerScenarios":"`{{ math.Floor \"n/a\" }}` or `{{ math.Floor .Params.value }}` where the param is unset or a non-numeric string.","commonSituations":"Computing row counts or truncated averages from front-matter data where some pages omit the numeric field; strings carrying units or currency symbols; passing an entire dict instead of one of its numeric values.","solutions":["Pass a numeric value: `{{ math.Floor 2.7 }}`.","Add a default for missing params: `{{ math.Floor (.Params.value | default 0) }}`.","Strip non-numeric characters and convert with `float` before calling."],"exampleFix":"<!-- before -->\n{{ math.Floor .Params.value }}\n<!-- after -->\n{{ math.Floor (.Params.value | default 0) }}","handlingStrategy":"type-guard","validationCode":"{{ $v := .Params.ratio }}\n{{ math.Floor (float $v) }}","typeGuard":"{{ with try (float $v) }}{{ if not .Err }}{{ math.Floor .Value }}{{ end }}{{ end }}","tryCatchPattern":"{{ with try (math.Floor $v) }}{{ if .Err }}{{ warnf \"Floor needs a float: %s\" .Err }}{{ else }}{{ .Value }}{{ end }}{{ end }}","preventionTips":["Same contract as Ceil: cast to float before calling math.Floor","Beware values coming from `index`/`.Get` in shortcodes — shortcode params are strings; cast them","Fail fast with errorf on truly invalid data instead of silently defaulting"],"tags":["hugo","templates","math","type-conversion","floor"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}