I get it that an int
can’t start with a 0 or a decimal, but is there a workaround for this?
Here’s my use case:
---
sizes: [".25", ".5", "1", "2"]
width: 600
---
{{ $sizes := .Params.sizes}}
{{ $width := .Params.width}}
This parameter is then ranged and each index of the array is multiplied
{{ $multiply_size := mul (int $width) (int (index $sizes $i )) }}
All of this works as long as it’s a whole number. Any ideas on a workaround to get decimals to work in this scenario?
Jura
March 2, 2018, 8:02am
2
When you multiply an integer with an integer, you get an int
as the result.
But when you multiply an integer with a decimal value, you get a decimal value as the result.
You can try something like this:
{{ $widthDecimal := mul (int $width) 1.0 }}
{{ $multiply_size := mul $widthDecimal (int (index $sizes $i )) }}
1 Like
What @Jura said is my go-to truck as well.
In this case though, you simply need to use float
instead of int
.
(float foo)
would be equivalent to (mul foo 1.0)
.
1 Like
Also, do you need the sizes to be strings? Unfortunately, Hugo converts them to strings anyways internally (ref ) but for clarity you can write:
sizes: [0.25, 0.5, 1.0, 2.0]
Jura
March 2, 2018, 8:42am
5
I didn’t even realise the float
function. Thanks!
With float
the code simply becomes if I’m not mistaken:
{{ $multiply_size := mul (int $width) (float (index $sizes $i )) }}
2 Likes
Thanks @Jura and @kaushalmodi using float did the trick!