gohugoio/hugo · error

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

Error message

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

What it means

math.Ceil converts its argument to float64 via cast.ToFloat64E and returns this error when the conversion fails. Despite the wording ('non-float'), any castable numeric — int, float, or numeric string — is accepted; the error means the input was not numeric at all.

Source

Thrown at tpl/math/math.go:105

// Atan2 returns the arc tangent of n/m, using the signs of the two to determine the quadrant of the return value.
func (ns *Namespace) Atan2(n, m any) (float64, error) {
	afx, err := cast.ToFloat64E(n)
	if err != nil {
		return 0, errors.New("requires numeric arguments")
	}
	afy, err := cast.ToFloat64E(m)
	if err != nil {
		return 0, errors.New("requires numeric arguments")
	}
	return math.Atan2(afx, afy), nil
}

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

	return math.Ceil(xf), nil
}

// 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, '/')
}

View on GitHub ↗ (pinned to 8a468df065)

Solutions

  1. Ensure the argument is numeric: `{{ math.Ceil 2.4 }}`.
  2. Convert explicitly first: `{{ math.Ceil (float $v) }}` or supply `| default 0` for possibly-missing params.
  3. Normalize locale strings (replace comma with dot) before converting.

Example fix

<!-- before -->
{{ math.Ceil .Params.ratio }}  {{/* ratio: "1,5" */}}
<!-- after -->
{{ math.Ceil (float (replace .Params.ratio "," ".")) }}
Defensive patterns

Strategy: type-guard

Validate before calling

{{ $v := .Params.price }}
{{ math.Ceil (float $v) }}

Type guard

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

Try / catch

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

Prevention

When it happens

Trigger: `{{ math.Ceil "2.x" }}`, `{{ math.Ceil .Params.ratio }}` where the param is nil, a non-numeric string, a slice, or a map.

Common situations: Pagination or grid math (`math.Ceil (div (len .Pages) 3)`) where an upstream value is a string from front matter; missing params resolving to nil; locale-formatted numbers with commas ("1,5") that cast rejects.

Related errors


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