joe
joe

Reputation: 103

keep negative number in string negative when converted golang

I'm trying to extract LAT / LONG from a string, but my negative float for the longitude keeps changing to zero, so far I have:

loc1 := "33.333333"

loc2 := "-44.44444"

However when using the strvconv package my results:

nloc1, _ := strconv.ParseFloat(loc1, 64)

RESULT: 33.333333 (successful float64)


nloc2, _ := strconv.ParseFloat(loc2, 64)

RESULT: 0 (successful float64)

any ideas on how to stop that from going to zero? I need to keep it the same number just a negative float. Thanks in advance!

EDIT:: The issue was a leading space, once reformatted, it works as it should. Thanks all.

Upvotes: 5

Views: 2228

Answers (1)

Coyote
Coyote

Reputation: 2524

This usually happens when you have leading or trailing whitespaces.

Try this:

nloc2, _ := strconv.ParseFloat(strings.TrimSpace(loc2), 64)

Upvotes: 6

Related Questions