Hugo Formating distance in km

Hi there,

In my page frontmatter I add values such as

# if the value is 1 in the page layout I show it as 1 km
1

# if the value is 0.25 I currently show it as 0.25 km
0.25

is it posible to tell Hugo to display 0.25 km as 250 meters by default the same way it turn 1 to 1st etc?

Thanks

Nope. (The 1 => 1st mapping is quite useless if your site is not in English). And the relation between 0.25/250 is far less obvious than that between 1/1st. How would anyone know if/when you want kilometers displayed as meters? Not even booking.com bothers with this kind of conversion, they display all distances as 0.x km (from center or whatever).

1 Like

Ok

So something like this is my only option

{{ with .Params.distance }}
{{ if ge . 1.0 }}
{{ printf "%d km" . }}
{{ else }}
{{ printf "%d meters" (mul . 1000) }}
{{ end }}
{{ end }}

but then I am not able to figure out how to use the i18n “km” instead of the simple km text.

any solution on this? thanks

The above code didnt work for me so I instead use this code:

{{ with .Params.distance }}
{{ if ge . 1 }}
{{ . }} {{ i18n "km" }}
{{ else }}
{{ mul . 1000 }} {{ i18n "meters" }}
{{ end }}
{{ end }}

Hope this helped someone.

Thanks!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.