Removing leading 0 from number in string, int function error


#1

Hi all,

I got into an issue with the int function.
I used this int to remove the leading 0 from a date in string format and it works perfectly for all numbers except for 08 and 09.

the code is {{ int "09" }} and it throws this error:

ERROR 2017/08/10 14:32:19 Error while rendering "page": template: /path/list.html:15:11: executing "main" at <int "08">: error calling int: unable to cast "08" of type string to int

I don’t know if this is a bug or if I was lucky to get it work from 1 to 7, but if it isn’t is there another, and safer, way to remove a leading 0 in hugo?

thanks for the help!


Unable to cast "09" of type string to int
#2

It seems ‘int’ treats the number as octal, so you only get 0…7 symbols. There must be a way to tell int to treat the number as a decimal, but I can’t info on that.


Unable to cast "09" of type string to int
#3

Try slicestr.

 {{ $test := slicestr "07" 1 }}
 <p>This will be seven: {{ $test }}</p>

 {{ $test := slicestr "08" 1 }}
 <p> This will be eight: {{ $test }}</p>

#4

thanks for your reply @Akronym_Rising.
unfortunately I have to remove the leading number only if 0. I use the output for composing a date.

for more context, I use a string date value like date = "2017-03-12 and then to print it out:

{{ $date := split .context.date "-" }}
{{ $day := index $date 2 }}
{{ $month := index $date 1 }}
{{ $year := index $date 0 }}

so, in the end the only solution I found is this:

{{ if or (eq $day "10") (eq $day "20") (eq $day "30") }}
  {{ $day }}
{{ else }}
  {{ trim $day "0" }}
{{ end }}

{{ if eq $month "10" }}
  {{ index .months (sub (int $month) 1) }}
{{ else }}
  {{ index .months (sub (int (trim $month "0")) 1) }}
{{ end }}

{{ $year }}

this was possible only because I know days cannot go over 31 and months over 12 :slight_smile:

still an ugly solution :frowning:


#5

We need to add the strings.TrimLeft func to our templates, but if you know that there will only be a single leading zero, you can use strings.TrimPrefix. And I just realized that we haven’t documented TrimPrefix or TrimSuffix.

So, here’s what I’d do to solve your problem:

{{ $day := int strings.TrimPrefix "0" (index $date 2) }}

#6

thank you @moorereason!
it works, but your example got me first with an error for the int function (3 args instead of 1), and solved it with parentesis.
then I got only zeroes, so I figured out there was an error in the order of the args.

the final working code is this:

{{ $day := int (strings.TrimPrefix (index $date 2) "0") }}

thank you very much, I would have hated to leave the hardcoded condition on the code.


#7

It “seemed” such and easy problem at first. Glad this thread is getting somewhere. I also found this:

findRE .mycontent (index .Params 1 | default 1 | int)

#8

FWIW: Here’s how to do it in Go:
https://golang.org/pkg/strconv/


#9

@yesh, I got those parameters backwards on TrimPrefix because I implemented it backwards! Doh!

We generally put the target variable as the last input so that you can use pipes, {{ $num | strings.TrimPrefix "0" }}. Since we haven’t published TrimPrefix and TrimSuffix in the docs yet, I’ll fix the function soon, so expect this section of your code to break when we push out the next release.


#10
{{ replaceRE "^0" "" "07" }}

#11

Except regex is slower than simple string manipulations.


#12

Generally, yes, but usually not enough to matter compared to the rest of the site build process. For instance, I build an alphabetical index of paginated recipes using:

{{ $i := (substr .Title 0 1 | lower | replaceRE "[^a-z]" "#") }}

It runs at least once per page for ~18,000 pages, and adds a fraction of a second to the 22-second site build time.

-j


#13

ok, will take in mind for the next update!

thanks,
yeshe


#14

Hugo v0.27 was released yesterday. Includes new strings.TrimLeft and TrimRight functions and fixes the TrimPrefix and TrimSuffix parameter ordering.