Outputting numbers with thousand separators

I read here on SO that Go’s fmt command (used by Hugo’s printf) doesn’t support thousand separators. Would it perhaps be possible to add that to Hugo?

We can already format dates, and I think it will be valuable if we can format numbers too. For example, instead of printing 2354098 formatting it to 2,354,098. That latter format is much easier to read.

1 Like

Agree. This one looks solid enough:

(gotta love the license)


Thanks. I’ve made a Github issue here to track/manage this feature.

What’s the status of this feature request I wonder?

  • The release log for Hugo 0.16 speaks about the humanize function and links to this discussion.
  • That discussion speaks about the ‘numFormat’ function, which is given examples in a commit from digitalcraftsman here.
  • The examples from digitalcraftsman are however not shown on the functions documentation page (despite accepted, it seems).
  • For someone who doesn’t have much experience with Github pulls/requests and so on, it seems that the ‘numFormat’ function is made and accepted.
  • But the humanize function does, according to the documentation on functions, something else than the request for ‘numFormat’ does.

The numFormat function at least doesn’t work with 0.16, for me:

ERROR: 2016/06/20 16:38:10 template.go:472: template: theme/index.html:128: function “numFormat” not defined

I think I’m misunderstanding things. Perhaps the ‘numFormat’ feature is combined with humanize? If so, how?

Hello @Jura,

the pull request for the numFormat template function is still open. This means it hasn’t been merged in the codebase of Hugo so far. Therefore, you can’t call the function and Hugo 0.16 raises an error.

Thanks for explaining. I didn’t get that since it was linked to from the Hugo 0.16 release page:

Reading release notes is harder than I thought. :slightly_smiling:

It looks like the template function was associated with the wrong pull request by accident. We should fix the release notes. This should the correct one:

Oops. That was my typo. Fixed in the docs site and the Github release notes.

Thank you for the fix.

What’s the status on this feature request? Still worthwhile or not?

As a user I’d still like to use such a feature, but see that from a development standpoint @moorereason’s pull request is still pending. Would be happy to help, but unsure what to do since it seems to be in limbo land now. :slightly_smiling:

Okay, this is ugly, and likely quite slow, but if you have a small number of positive integers you’d like to add commas to, it works. It could easily be extended to handle floating-point numbers with split, but I’ll leave that for someone more motivated. :slightly_smiling:

In a template:

{{ $.Scratch.Set "num" 1234567 }}
{{ partial "prettynum.html" . }}


{{- $num := (string ($.Scratch.Get "num")) -}}
{{- if gt ($.Scratch.Get "num") 999 -}}
  {{- $n := (div (len $num) 3) -}}
  {{- $.Scratch.Set "tmp" (substr $num 0 (sub (len $num) (mul $n 3))) -}}
  {{- $offset := len ($.Scratch.Get "tmp") -}}
  {{- range $i := seq 1 $n -}}
    {{- if gt (len ($.Scratch.Get "tmp")) 0 -}}
      {{- $.Scratch.Add "tmp" "," -}}
    {{- end -}}
    {{- $.Scratch.Add "tmp" (substr $num (add $offset (mul (sub $i 1) 3)) 3) -}}
  {{- end -}}
  {{- $.Scratch.Get "tmp" -}}
{{- else -}}
  {{- $num -}}
{{- end -}}

Or use lang.NumFmt in Hugo v0.21+. :wink:

Missed that line in the release notes… :slightly_smiling:

Also, the search engine for the old docs site won’t find it unless you already know its name, and it doesn’t look like it’s in the new docs site yet. The forum search just turned up this thread, of course.

Oh, well, it was an interesting experiment in working around the limitations in template functionality. Almost as much fun as building a self-contained “jump to random content page” button in my navbar (modern browsers handle huge Javascript arrays surprisingly well…).