8000 Support converting string float numbers to integer types by sagikazarmark · Pull Request #261 · spf13/cast · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Support converting string float numbers to integer types #261

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

Merged
merged 1 commit into from
May 31, 2025

Conversation

sagikazarmark
Copy link
Collaborator

Fixes #204
Fixes #189
Fixes #172
Fixes #210
Fixes #170

Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
@sagikazarmark sagikazarmark changed the title Support converting float string numbers to integer types Support converting string float numbers to integer types May 31, 2025
@sagikazarmark sagikazarmark merged commit b4445f6 into master May 31, 2025
18 checks passed
@sagikazarmark sagikazarmark deleted the float-string branch May 31, 2025 15:23
@@ -400,7 +401,7 @@ func parseInt[T integer](s string) (T, error) {
}

func parseUint[T unsigned](s string) (T, error) {
v, err := strconv.ParseUint(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseUint(trimDecimal(s), 0, 0)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm suspicious here

It works for the expected case. The one that caused to code this.

But what about the string "10...17" previously it was returning an error, now it will happily return 10

https://go.dev/play/p/bkiLc4jdBEH

vs

https://go.dev/play/p/VyqcbrcMEWk

The issue is worst if you consider:

  • 10.foobar (invalid)
  • 10.0i (a complex)
  • 10.0E9 (unsupported syntax previously)

All of them are now returning 10

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. Let me try to fix that.

cloudxxx8 added a commit to cloudxxx8/device-rest-go that referenced this pull request Jun 9, 2025
Fixed the unit tests to meet the change of spf13 v1.9
spf13/cast#261

Signed-off-by: Cloud Tsai <cloudxxx8@gmail.com>
judehung pushed a commit to edgexfoundry/device-rest-go that referenced this pull request Jun 9, 2025
Fixed the unit tests to meet the change of spf13 v1.9
spf13/cast#261

Signed-off-by: Cloud Tsai <cloudxxx8@gmail.com>
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

Successfully merging this pull request may close these issues.

cast.ToInt("25.400") returned 0 cast.ToInt not parsing float like string json.Number support acts like string=>int64, instead of float64=>int64
2 participants
0