Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Casting a string to int with a 0 in prefix does not work as expected #74

Open
cbajohr opened this issue Mar 11, 2019 · 1 comment
Open

Comments

@cbajohr
Copy link

cbajohr commented Mar 11, 2019

When I cast a string with prefixed 0 like "0123" with cast.ToInt("0123"), it results into 83 instead of the expected 123

Example code:

package main

import (
	"fmt"
	"github.com/spf13/cast"
)

func main() {
	numb := "0123"
	castedNumb := cast.ToInt(t)
	fmt.Println(numb, castedNumb)
}
@cbajohr cbajohr changed the title Casting string with 0 in prefix to int does not work Casting a string to int with a 0 in prefix does not work as expected Mar 11, 2019
@cbajohr
Copy link
Author

cbajohr commented Mar 12, 2019

The problem is the strconv.ParseInt(s, 0, 0) in the ToInt methods. When I use strconv.ParseInt(s, 10, 0) instead it works like a charm.

Like the strconv.ParseInt comment says:

If base == 0, the base is implied by the string's prefix: base 16 for "0x", base 8 for "0", and base 10 otherwise. For bases 1, below 0 or above 36 an error is returned.

I think strconv.ParseInt(s, 10, 0) is the right expected behavior of a standard ToInt casting. I will add a pull request later today, to fix that problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant