gohugoio/hugo · error

Floor operator can't be used with non-float value

Error message

Floor operator can't be used with non-float value

What it means

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).

Source

Thrown at tpl/math/math.go:129

// Cos returns the cosine of the radian argument n.
func (ns *Namespace) Cos(n any) (float64, error) {
	af, err := cast.ToFloat64E(n)
	if err != nil {
		return 0, errors.New("requires a numeric argument")
	}
	return math.Cos(af), nil
}

// Div divides n1 by n2.
func (ns *Namespace) Div(inputs ...any) (any, error) {
	return ns.doArithmetic(inputs, '/')
}

// Floor returns the greatest integer value less than or equal to n.
func (ns *Namespace) Floor(n any) (float64, error) {
	xf, err := cast.ToFloat64E(n)
	if err != nil {
		return 0, errors.New("Floor operator can't be used with non-float value")
	}

	return math.Floor(xf), nil
}

// Log returns the natural logarithm of the number n.
func (ns *Namespace) Log(n any) (float64, error) {
	af, err := cast.ToFloat64E(n)
	if err != nil {
		return 0, errors.New("Log operator can't be used with non integer or float value")
	}

	return math.Log(af), nil
}

// Max returns the greater of all numbers in inputs. Any slices in inputs are flattened.
func (ns *Namespace) Max(inputs ...any) (maximum float64, err error) {
	return ns.applyOpToScalarsOrSlices("Max", math.Max, inputs...)

View on GitHub ↗ (pinned to 8a468df065)

Solutions

  1. Pass a numeric value: `{{ math.Floor 2.7 }}`.
  2. Add a default for missing params: `{{ math.Floor (.Params.value | default 0) }}`.
  3. Strip non-numeric characters and convert with `float` before calling.

Example fix

<!-- before -->
{{ math.Floor .Params.value }}
<!-- after -->
{{ math.Floor (.Params.value | default 0) }}
Defensive patterns

Strategy: type-guard

Validate before calling

{{ $v := .Params.ratio }}
{{ math.Floor (float $v) }}

Type guard

{{ with try (float $v) }}{{ if not .Err }}{{ math.Floor .Value }}{{ end }}{{ end }}

Try / catch

{{ with try (math.Floor $v) }}{{ if .Err }}{{ warnf "Floor needs a float: %s" .Err }}{{ else }}{{ .Value }}{{ end }}{{ end }}

Prevention

When it happens

Trigger: `{{ math.Floor "n/a" }}` or `{{ math.Floor .Params.value }}` where the param is unset or a non-numeric string.

Common situations: 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.

Related errors


AI-assisted analysis of gohugoio/hugo@8a468df065 (2026-07-31). Data as JSON: /data/errors/173f2e785cef5b1b.json. Report an issue: GitHub ↗.