strconv.ParseFloat turning values to 0 when it shouldn't

759 views Asked by At

I have the following code:

buffer := make([]byte, 256)
conn.Read(buffer)

result:= string(buffer)
fmt.Println("Val:", result)

floatResult, _ := strconv.ParseFloat(result, 64)
fmt.Println("Val on float:", floatResult)

The output is the following:

Val: 40.385167
Val on float: 0

This seems the correct way to parse string into float64's, is there something i'm missing?

1

There are 1 answers

0
kozmo On

You can create new slice with len equals read bytes (look at Reader)

buffer := make([]byte, 256)
i, _ := conn.Read(buffer) // i  number of read bytes 

result:= string(buffer[:i]) // create new slice with 'len' equals number of read bytes
fmt.Println("Val:", result)

floatResult, _ := strconv.ParseFloat(result, 64)
fmt.Println("Val on float:", floatResult)

PLAYGROUND (io.Reader stub)