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
Answer from Toni Villena on Stack OverflowThe 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
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
strconv.ParseFloat() faster altrernatives
proposal: strconv: add ParseFloatPrefix
strconv: no way to ParseFloat / ParseInt from []byte
strconv: inaccurate string to float64 conversion ParseFloat
I am parsing huge json files with my own parser where all values are float, which I use in math calculations further on. pprof profiling says 60% of the time is used by the strconv.ParseFloat(). I looked into the code of the ParseFloat() and it's pretty comprehensive, not much clue on my side. Also I checked github and goolged it, but no results suggesting anything else. I believe the strconv.ParseFloat() is probably pretty optimized but nevertheless, I just wonder if anyone has some faster alternative suggestions to the strconv.ParseFloat()? Thanks..