Do math functions take decimals?

I get an error when I try to divide a number by a decimal. I am guessing coz some results are long string of numbers? For example, I tried dividing the image width by 1.45 and it returned an error div .Width 1.45.

 execute of template failed at <.Fill>: error calling Fill: strconv.Atoi: parsing "%!d(float64=448.5714285714286)": invalid syntax

Edit: Even 2.0 did not work, but 2 does.

execute of template failed at <.Fill>: error calling Fill: strconv.Atoi: parsing "%!d(float64=314)": invalid syntax

If one of the numbers is a float, the result is a float.

It seems like you are trying to pass a float into the printf function using the %d token. The %d token is for integers not floats.

I am using your solution. So, what should I convert it to?

In that example you were doing integer division, not float division.

Convert your float to int using any of:

None of those will help me coz I needed the exact number 1.45 not a rounded off number.

Cast the result, not the divisor.

Kindly share an example. This is beyond my knowledge at this point. I just want to standardize my image width and height based on the current image size for single and list pages.

{{- with or (.Resources.GetMatch "featured.*") (resources.Get "image/featured.jpg") }}
  {{- with .Fill (printf "%dx%d TopLeft" (div .Width 1.45 | int) (div .Height 1.45 | int)) }}
    <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
  {{- end }}
{{- end }}
2 Likes

My mistake was doing (div .Height (int 1.45) ). I was following the int page wrong then.

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