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
go - How to convert float to string - Stack Overflow
strconv: inaccurate string to float64 conversion ParseFloat
go - How to convert []string to []float64 in Golang? - Stack Overflow
Problem converting float32 to string
Use strconv.FormatFloat like such:
s := strconv.FormatFloat(3.1415, 'f', -1, 64)
fmt.Println(s)
Outputs
3.1415
Convert float to string
FormatFloat converts the floating-point number f to a string, according to the format fmt and precision prec. It rounds the result assuming that the original was obtained from a floating-point value of bitSize bits (32 for float32, 64 for float64).
func FormatFloat(f float64, fmt byte, prec, bitSize int) string
f := 3.14159265
s := strconv.FormatFloat(f, 'E', -1, 64)
fmt.Println(s)
Output is "3.14159265"
Another method is by using fmt.Sprintf
s := fmt.Sprintf("%f", 123.456)
fmt.Println(s)
Output is "123.456000"
Check the code on play ground
You need to convert string to float64 using strconv.ParseFloat function:
package main
import (
"fmt"
"os"
"strconv"
)
var numbers []float64
var sum float64 = 0
func main() {
if len(os.Args) <= 1 {
return
}
for _, arg := range os.Args[1:] {
if n, err := strconv.ParseFloat(arg, 64); err == nil {
numbers = append(numbers, n)
}
}
fmt.Println("Numbers are: ", numbers)
for _, value := range numbers {
sum += value
}
}
It can't convert because string and int are not compatible.
Instead of having the numbers slice, just iterate over os.Args[1:], using ParseFloat from the strconv package.
fmt.Print("Numbers are: ")
for _, arg := range os.Args[1:] {
fmt.Print(arg, " ")
value, err := strconv.ParseFloat(arg, 64)
if err != nil {
panic(err)
}
sum += value
}
What is the easy way of converting a float to a string?
The strconv package seems awfully cluttered and there doesn't seem to be a function where you can simply give a float and get a string