gohugoio/hugo · error
Round operator can't be used with non-float value
Error message
Round operator can't be used with non-float value
What it means
Thrown by math.Round when its single argument cannot be converted to float64 by cast.ToFloat64E. Round rounds half away from zero and needs a numeric input; booleans, nil, structs, slices, and non-numeric strings all fail conversion.
Solutions
- Cast or default the input: {{ math.Round (float ($v | default 0)) }}.
- Guard with an if block: {{ if $v }}{{ math.Round $v }}{{ end }}.
- Fix front matter to use numeric types instead of quoted strings.
Example fix
// before
{{ math.Round .Params.rating }}
// after
{{ with .Params.rating }}{{ math.Round . }}{{ end }} Defensive patterns
Strategy: validation
Validate before calling
{{ with $v }}{{ math.Round (float .) }}{{ end }} Prevention
- Wrap math.Round in an {{ with }} block to skip nil values.
- Cast with (float ...) when the source is a string param.
- Use numeric front matter types.
When it happens
Trigger: Calling {{ math.Round "foo" }}, {{ math.Round $nilVar }}, or {{ math.Round .Params.someUndefinedKey }}. Also when a front matter field is a date or duration object rather than a number.
Common situations: Rounding a value pulled from params that was authored as a string; passing a time.Time or resource object by mistake; conditional sections where the value is unset on some pages.
Related errors
- Pow operator can't be used with non-float value
- Sqrt operator can't be used with non integer or float value
- can't divide the value by 0
- Ceil operator can't be used with non-float value
- end argument must be integer
AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09).
Data as JSON: /api/errors/3bed0b9c9eb84709.
Report an issue: GitHub.
Appendix: source
Thrown at tpl/math/math.go:225
// Product returns the product of all numbers in inputs. Any slices in inputs are flattened.
func (ns *Namespace) Product(inputs ...any) (product float64, err error) {
fn := func(x, y float64) float64 {
return x * y
}
return ns.applyOpToScalarsOrSlices("Product", fn, inputs...)
}
// Rand returns, as a float64, a pseudo-random number in the half-open interval [0.0,1.0).
func (ns *Namespace) Rand() float64 {
return rand.Float64()
}
// Round returns the integer nearest to n, rounding half away from zero.
func (ns *Namespace) Round(n any) (float64, error) {
xf, err := cast.ToFloat64E(n)
if err != nil {
return 0, errors.New("Round operator can't be used with non-float value")
}
return _round(xf), nil
}
// Sin returns the sine of the radian argument n.
func (ns *Namespace) Sin(n any) (float64, error) {
af, err := cast.ToFloat64E(n)
if err != nil {
return 0, errors.New("requires a numeric argument")
}
return math.Sin(af), nil
}
// Sqrt returns the square root of the number n.
func (ns *Namespace) Sqrt(n any) (float64, error) {
af, err := cast.ToFloat64E(n)
if err != nil {View on GitHub (pinned to 52c9bd7908)