Marshall Bowers

Conjurer of code. Devourer of art. Pursuer of æsthetics.

Formatting Numbers: Revisited

Friday, August 27, 2021
185 words
1 minute read

A few months ago I asked "how hard is it to format a number?". The solution I came up with at the time was to use some light JavaScript to format the numbers using Number.prototype.toLocaleString().

Of course, this was not good enough for me.

Ideally, I would like to use a Tera filter to format the numbers at site generation time:

{% set word_count = 9001 %}
{{ word_count | num_format }}
<!-- 9,001 -->

This is incredibly reminiscent of how formatting dates works with Tera's date filter.

I ended up adding a num_format filter to Zola, powered by the excellent crate of the same name.

As I write this, I have since updated my stats page to take advantage of this new filter. Inspect the source and you'll see that the numbers are nicely pre-formatted in the HTML—no JavaScript required!