Converting numbers to currency

In front matter I have:

  currency = "USD"
  minvalue = 10000
  maxvalue = 20000

The result I’m looking for is: $10K - $20K or if there is only a minvalue it would be $10K.

In layout/single.html, I’m doing the following:

{{$currencySymbol := dict "EUR" "€" "GBP" "£" "USD" "$"}}
{{$.Scratch.Set "currencySymbol" (.Params.price.currency)}}
{{range $k, $v := $currencySymbol}}
  {{$.Scratch.Set "currencySymbol" (replace ($.Scratch.Get "currencySymbol") $k $v)}}
{{$minValue := printf "%s%s" ($.Scratch.Get "currencySymbol") (replaceRE "000$" "K" .Params.price.minvalue)}}
{{$maxValue := printf "%s%s" ($.Scratch.Get "currencySymbol") (replaceRE "000$" "K" .Params.price.maxvalue)}}

{{if .Params.price.maxvalue}}
  {{printf "%s - %s" $minValue $maxValue}}
{{else if .Params.price.minvalue}}

This code works, but is there a more elegant way of achieving the same result?

1 Like