How to calculate with floating values in Hugo?

Hello,

I was working with the div math feature and noticed that, while it divides integers, it returns an integer also. I want to calculate percentages in Hugo, and thus need floating values. Is that possible?

@tatsushid would know the details, but I just added

To show that float calculations works in general.

But division with two integers will give integer division, I guess, and I’m not sure how to “cast” to float in a template.

Thanks, then I’ll just multiply the numerator by 100 to get the approximate percentage (accurate enough for my goal):

{{ div (mul $done 100) $total }}% done
1 Like

@jura, in your example, a floating value will be returned if 100 is replaced by 100.0 like

{{ div (mul $done 100.0) $total }}% done

doArithmetic internal function converts integer to float if one of arguments includes a float value.

2 Likes