Hello team,
I have a cast error on an int.
unable to cast "09" of type string to int
Here is the code used, you have an idea of how to solve this problem?
+++
cityZipCode = "75009"
+++
{{ if (int (last 2 .Params.cityZipCode)) eq 1 }}er{{else}}e{{end}}
Thank you in advance: p
As it’s been said before int
treats the number as octal
, so you only get 0…7 symbols.
See this thread for more info and how to remove the zeros.
I ain’t got a clue how you could convert the 9 decimal to its octal
value with existing Hugo functions.
2 Likes
That’s really strange! Good to know…
I hope this gets fixed in hugo. If not that, then at least mention this quirk in the doc: https://gohugo.io/functions/int/
Is this a bug? Or is it that the Go/Web development community tend to think of integers in octals?
This octal thing definitely looks like a bug. This example shows that "8"
casts to 8
: https://github.com/spf13/cast/blob/master/README.md
So can try removing leading zeros using replaceRE
and then do the cast?
Not a bug but a strange feature nonetheless:https://golang.org/pkg/strconv/#ParseInt
the base is implied by the string’s prefix: base 16 for “0x”, base 8 for “0”, and base 10 otherwise.
That’s a really really bad design decision IMO… couldn’t they simply make the prefix for octals to be "0o"
(“o” for octal) or something else would wouldn’t false match decimal numbers? Having decimal numbers with leading zeros is not very uncommon!
So yeah… removing the leading 0’s should do the trick.
Do this:
{{ if strings.HasSuffix "01" .Params.cityZipCode }}
1 Like