numFormat in Hugo to format number of zeros

Hi there,

How can I get 5.0 instead of 5 value in Hugo?

# current setup
{{ .rating }} = 5
# preferred setup
{{ .rating | numFormat something here }} = 5.0

Thanks

{{ printf “%.1f” (float .rating) }}

To clarify, you can do either of these:

{{ 5 | lang.FormatNumber 1 }}
{{ 5 | float | printf "%.1f" }}

The first one localizes the value (i.e., period vs. comma depending on locale), but the second is a little bit faster.

Note that the cast.ToFloat function takes only 1 argument.

thx to @jmooring for

  • the correction regarding float removed that from original post - dunno where I got that from :flushed:
  • the hint for the nice localization variant
1 Like