cast icon indicating copy to clipboard operation
cast copied to clipboard

cast.ToUint32("0134444") = 47396

Open bb7788 opened this issue 11 months ago • 3 comments

cast.ToUint32("0134444") = 47396

my go version go1.23.4 darwin/amd64, cast version 1.71

bb7788 avatar Jan 07 '25 04:01 bb7788

Most programming languages of the C-family including Go treat integer numbers as octal if starting with 0 and hex if starting with 0x. Here it is starting with 0 hence considered as octal but decimal and resulted into different number than your expectation.

yamadev11 avatar Jan 08 '25 17:01 yamadev11

The reason this is happening is due to the 0 as the 2nd argument being used in the following lines:

https://github.com/spf13/cast/blob/01004f2c354f73d3cb4651ad7360ad03fb588e79/caste.go#L615-L620

If used as:

v, err := strconv.ParseUint(trimZeroDecimal("0134444"), 10, 0) 
if v == nil {
   fmt.Println(v)
}

This returns Uint(134444). https://goplay.tools/snippet/4iuNrPt2jDb

I've submitted a PR to address this so you can use this package and get unblocked.

andreimerlescu avatar May 29 '25 12:05 andreimerlescu

See https://pkg.go.dev/strconv#ParseInt

andig avatar Jul 09 '25 17:07 andig