25

I've just started learning Go and I'm trying to convert a string from standard input to a float64 so I can perform an arithmetic operation on the input value.

The output returns "0 feet converted to meters gives you 0 meters" regardless of the input value. I can't figure out why the value is zero after invoking ParseFloat on the input.

If someone could please point out to me why this is happening, I would greatly appreciate it.

const conversion float64 = 0.3048

func feetToMeters (feet float64) (meters float64) {
  return feet * conversion
}

func main(){
  fmt.Println("\n\nThis program will convert feet to meters for you!\n")

  reader := bufio.NewReader(os.Stdin)
  fmt.Println("Enter feet value: \n")
  feet, _ := reader.ReadString('\n')

  feetFloat, _ := strconv.ParseFloat(feet, 64)

  meters := feetToMeters(feetFloat)

  fmt.Printf("%v feet converted to meters give you %v meters",feetFloat,meters)
}
3
  • func main(){ fmt.Println("\n\nThis program will convert feet to meters for you!\n") feet := "35" feetFloat, _ := strconv.ParseFloat(feet, 64) meters := feetToMeters(feetFloat) fmt.Printf("%v feet converted to meters give you %v meters",feetFloat,meters) } I modified to this and it gives me the right answer. The problem ought to be with the reader. What happens if you fmt.Println(feet)? Commented Aug 4, 2016 at 22:01
  • 1
    Thank you @jonathanGB, I realize now that it was printing a new line character as well. Commented Aug 4, 2016 at 23:14
  • @XyMcXface Thank you for the advice. I'll have to learn about handling errors next to avoid further headache! Commented Aug 4, 2016 at 23:15

2 Answers 2

39

The problem is that you try to parse "x.x\n", e.g: 1.8\n. And this returns an error: strconv.ParseFloat: parsing "1.8\n": invalid syntax. You can do a strings.TrimSpace function or to convert feet[:len(feet)-1] to delete \n character

With strings.TrimSpace() (you need to import strings package):

feetFloat, _ := strconv.ParseFloat(strings.TrimSpace(feet), 64)

Wtih feet[:len(feet)-1]:

feetFloat, _ := strconv.ParseFloat(feet[:len(feet)-1], 64)

Output in both cases:

10.8 feet converted to meters give you 3.2918400000000005 meters
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Toni, I didn't even think about the fact that there was a new line character in the string. I appreciate you give me two solutions!
TrimSpace is a life savior. I thought the ParseFloat will be handling that. :)
2

just tested this solution and also added one more feature:

func lbsToGrams(lbs float64) (grams float64) {
    return lbs * conversionWeight
}

Find out more on my github here

2 Comments

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
Thanks for checking out. It's a confirmation that the above works and it's additional golang example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.