{"id":"3bed0b9c9eb84709","repo":"gohugoio/hugo","slug":"round-operator-can-t-be-used-with-non-float-value","errorCode":null,"errorMessage":"Round operator can't be used with non-float value","messagePattern":"Round operator can't be used with non-float value","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/math/math.go","lineNumber":225,"sourceCode":"\n// Product returns the product of all numbers in inputs. Any slices in inputs are flattened.\nfunc (ns *Namespace) Product(inputs ...any) (product float64, err error) {\n\tfn := func(x, y float64) float64 {\n\t\treturn x * y\n\t}\n\treturn ns.applyOpToScalarsOrSlices(\"Product\", fn, inputs...)\n}\n\n// Rand returns, as a float64, a pseudo-random number in the half-open interval [0.0,1.0).\nfunc (ns *Namespace) Rand() float64 {\n\treturn rand.Float64()\n}\n\n// Round returns the integer nearest to n, rounding half away from zero.\nfunc (ns *Namespace) Round(n any) (float64, error) {\n\txf, err := cast.ToFloat64E(n)\n\tif err != nil {\n\t\treturn 0, errors.New(\"Round operator can't be used with non-float value\")\n\t}\n\n\treturn _round(xf), nil\n}\n\n// Sin returns the sine of the radian argument n.\nfunc (ns *Namespace) Sin(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.Sin(af), nil\n}\n\n// Sqrt returns the square root of the number n.\nfunc (ns *Namespace) Sqrt(n any) (float64, error) {\n\taf, err := cast.ToFloat64E(n)\n\tif err != nil {","sourceCodeStart":207,"sourceCodeEnd":243,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/tpl/math/math.go#L207-L243","documentation":"Hugo's `math.Round` casts its single argument to float64 via `cast.ToFloat64E` and then rounds half away from zero using an internal `_round` helper. When the argument isn't convertible to a float (non-numeric string, nil, composite value), Hugo returns this error.","triggerScenarios":"`{{ math.Round X }}` where X is nil or a non-numeric string: `{{ math.Round \"1.5em\" }}`, `{{ math.Round .Params.score }}` with a missing param, or rounding the output of a string-producing function like `printf` without converting back to a number.","commonSituations":"Rounding computed values that were formatted to strings earlier in the pipeline (e.g. via `printf \"%.2f\"`); front-matter numbers stored with units (\"3.5s\"); data-file values quoted as strings; nil from absent `.Params`/`.Site.Params` keys.","solutions":["Round before formatting, not after: keep the value numeric through the pipeline and apply `printf`/`lang.FormatNumber` last.","Cast explicitly: `{{ math.Round (float $v) }}`.","Default missing params: `{{ math.Round (default 0.0 .Params.score) }}`.","Strip units from data values or store them as plain numbers."],"exampleFix":"<!-- before -->\n{{ math.Round (printf \"%.2f\" $ratio) }}\n<!-- after -->\n{{ printf \"%.2f\" (math.Round $ratio) }}","handlingStrategy":"type-guard","validationCode":"{{ math.Round (float $x) }}","typeGuard":"func toFloat(v any) (float64, bool) {\n    f, err := cast.ToFloat64E(v)\n    return f, err == nil\n}","tryCatchPattern":"if _, err := ns.Round(x); err != nil {\n    // value wasn't float-convertible\n}","preventionTips":["Coerce with `float` before `math.Round`, especially values from `.Params` or JSON data files","Prefer `lang.FormatNumber` for display rounding — it handles conversion internally","Default missing params: `{{ $x := $.Params.score | default 0.0 }}`"],"tags":["hugo","go-templates","math","type-conversion"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}