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

Leading or trailing spaces raise error in ParseFloat #3959

Closed
gopherbot opened this issue Aug 16, 2012 · 2 comments
Closed

Leading or trailing spaces raise error in ParseFloat #3959

gopherbot opened this issue Aug 16, 2012 · 2 comments

Comments

@gopherbot
Copy link
Contributor

by qsb300:

What steps will reproduce the problem?
Passing a string with leading or trailing string to ParseFloat, ParseFloat doesn't give
the float number but raises errors for last three strings
in code show at 
http://play.golang.org/p/9R7YUJm_a2

package main

import "fmt"
import "strconv"
import "strings"

func main() {
    fs := [...]string{"1.2", " 1.2", "1.2 ", " 1.2 "}
    for pos, str := range fs {
        f, err := strconv.ParseFloat(str, 64)
        fmt.Println(pos, str, f, err)
    }
    for pos, str := range fs {
        f, err := strconv.ParseFloat(strings.TrimSpace(str), 64)
        fmt.Println(pos, str, f, err)
    }
}

What is the expected output?
Leading and trailing spaces should be allowed as in most other languages I have known.
No error should be raise.

What do you see instead?
ParseFloat raise errors for the last three strings

Which version are you using?  (run 'go version')
go1.0.2

Allowance of leading/trailing space is a great convenience to deal with fixed format
data file, otherwise TrimSpace always had to be called.
@cznic
Copy link
Contributor

cznic commented Aug 16, 2012

Comment 1:

IMO should be closed with "works as intended". Why should ParseFloat magically trim
white space when e.g. ParseInt doesn't?

@ianlancetaylor
Copy link
Member

Comment 2:

Use TrimSpace, as you said.

Status changed to WorkingAsIntended.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants