Comments (4)
The "is wrong" statement is a little bombastic -- but the current behaviour is certainly a little undocumented. But it's certainly in line with the corresponding documentation in Go's stdlib.
https://play.golang.org/p/fMXB0rEgN9Z
from cast.
The "is wrong" statement is a little bombastic -- but the current behaviour is certainly a little undocumented. But it's certainly in line with the corresponding documentation in Go's stdlib.
Okay, thank you very much for your reply, but for special values, this will not apply and the developer needs to deal with it by himself
from cast.
@liuaiyuan this isn't my library and I did not implement these, and I would probably also prefer that they behaved the way you describe, but that would probably be too much of a breaking change.
/cc @spf13
from cast.
you could create a pr :)
the problem could be fixed with changing the v, err := strconv.ParseInt(s, 0, 0)
's to v, err := strconv.ParseInt(s, 10, 0)
...
I did this and added to the tests for your scenario, and it works... what makes me hesitate to submit a pr is I do not know what this breaks for people, even though this "fix" is how I would expect it to work there's probably a lot I'm not considering...
I modified all the "to" functions and tests, but this is part of the diff to illustrate my meaning
@ -345,6 +349,10 @@ func TestToInt64E(t *testing.T) {
{true, 1, false},
{false, 0, false},
{"8", 8, false},
+ {"-8", -8, false},
+ {"0100", 100, false},
+ {"01234", 1234, false},
+ {"1234", 1234, false},
{nil, 0, false},
// errors
{"test", 0, true},
diff --git a/caste.go b/caste.go
index 70c7291..6ccc2d0 100644
--- a/caste.go
+++ b/caste.go
@@ -211,7 +211,7 @@ func ToInt64E(i interface{}) (int64, error) {
case float32:
return int64(s), nil
case string:
- v, err := strconv.ParseInt(s, 0, 0)
+ v, err := strconv.ParseInt(s, 10, 0)
if err == nil {
return v, nil
}
the new tests fail until parseint is changed, then they all succeed
from cast.
Related Issues (20)
- Add function to cast `interface{}` to `[]float64` HOT 1
- Add IntTobytes
- Unexpected results with padded strings HOT 3
- cast.tobool uint support HOT 1
- memory cost maybe big
- cast.ToInt not parsing float like string
- byte to int? HOT 1
- Can I convert to a pointer type?
- Why not support []uint64 type in ToStringSliceE function?
- cast toint32 but return zero
- cast typedefed value to uint failed HOT 2
- cast.ToInt("25.400") returned 0 HOT 2
- numeric type cast Support json.Number HOT 1
- cast.ToUint64("14017652235894064730") return 0 HOT 2
- cast.ToInt64E("") returns error while cast.ToInt64E(nil) doesn't
- cast.ToInt("08") return 0 and so on HOT 2
- Cannot cast alias variable to int32
- ToInt have bad case HOT 1
- cast string to uint64 and uint32 failed on arm32
- Add default value to To_____ methods
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cast.