gohugoio/hugo · error

too many fields in options parameter to NumFmt

Error message

too many fields in options parameter to NumFmt

What it means

Thrown by lang.FormatNumberCustom (NumFmt) at tpl/lang/lang.go:178 when the options string, split by the delimiter, yields more than three fields. The options string encodes at most three values: negative sign, decimal mark, and grouping separator. A fourth field has no meaning and is rejected.

Solutions

  1. Use exactly three fields max: negative sign, decimal mark, grouping separator (default is "- . ,").
  2. Remove extra/doubled spaces in the options string; trim trailing whitespace.
  3. If you need a different delimiter, pass it as the second options arg: {{ lang.NumFmt 2 1.5 "-|.|," "|" }}.
  4. Omit the options arg entirely to accept the defaults.

Example fix

// before
{{ lang.NumFmt 2 1234.56 "- . ,  " }}

// after
{{ lang.NumFmt 2 1234.56 "- . ," }}
Defensive patterns

Strategy: validation

Validate before calling

// Keep options to at most three fields (neg, dec, grp) with no extra delimiters:
{{ lang.NumFmt 2 1234.56 "- . ," }}
// Or use a custom delimiter and trim stray tokens:
{{ lang.NumFmt 2 1234.56 "-|.|." "|" }}

Type guard

// Go caller: validate the options string fields before FormatNumberCustom:
func validNumFmtOptions(s, delim string) bool {
    return len(strings.Split(s, delim)) <= 3
}

Prevention

When it happens

Trigger: Calling {{ lang.NumFmt 2 1234.56 "- . , " }} where an extra space creates a fourth empty field, or passing "- . , x" with four real fields; using a delimiter that does not match the intended split; double spaces producing empty tokens.

Common situations: Trailing or doubled spaces in the options string (each space is a delimiter by default), creating empty extra fields; copying format strings from another locale library with a different field count; misunderstanding that only three fields are allowed.

Related errors


AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09). Data as JSON: /api/errors/0e1c496d786f15f8. Report an issue: GitHub.

Appendix: source

Thrown at tpl/lang/lang.go:178

			delim = s
		}

		s, err := cast.ToStringE(options[0])
		if err != nil {
			return "", err
		}

		rs := strings.Split(s, delim)
		switch len(rs) {
		case 0:
		case 1:
			neg = rs[0]
		case 2:
			neg, dec = rs[0], rs[1]
		case 3:
			neg, dec, grp = rs[0], rs[1], rs[2]
		default:
			return "", errors.New("too many fields in options parameter to NumFmt")
		}
	}

	exp := math.Pow(10.0, float64(prec))
	r := math.Round(n*exp) / exp

	// Logic from MIT Licensed github.com/gohugoio/locales/
	// Original Copyright (c) 2016 Go Playground

	s := strconv.FormatFloat(math.Abs(r), 'f', prec, 64)
	L := len(s) + 2 + len(s[:len(s)-1-prec])/3

	var count int
	inWhole := prec == 0
	b := make([]byte, 0, L)

	for i := len(s) - 1; i >= 0; i-- {
		if s[i] == '.' {

View on GitHub (pinned to 52c9bd7908)