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):

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

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

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

Output in both cases:

Copy10.8 feet converted to meters give you 3.2918400000000005 meters
Answer from Toni Villena on Stack Overflow
🌐
Educative
educative.io › answers › how-to-use-the-strconvparsefloat-function-in-golang
How to use the strconv.ParseFloat() function in Golang
Lines 11 to 21: We define the main() function, variable str of string type, and assign a value to it. We pass the variable in the ParseFloat() function, which converts the str variable to a floating-point number with the precision defined by bitSize.
Discussions

strconv.ParseFloat() faster altrernatives
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… More on reddit.com
🌐 r/golang
9
2
October 4, 2022
parsing - Golang ParseFloat not accurate in example - Stack Overflow
Is ParseFloat() what I should be using in this scenario? If not, I'd love to hear a brief explanation on this, as I'm still a programmer in learning. ... I don't think you should use floats for currency values. ... So, for this example - the amount that the API is returning is unfortunately a string. I've done a quick work around (just to have the ability to check if the actual amount is the same as the string amount). Example is here: play.golang... More on stackoverflow.com
🌐 stackoverflow.com
strconv: no way to ParseFloat / ParseInt from []byte
The strconv package's ParseFloat / ParseInt only take string. To reduce allocations, I need to parse an int from a []byte. I attempted to convert strconv's Parse internals to use []byte ins... More on github.com
🌐 github.com
3
January 20, 2020
proposal: strconv: add ParseFloatPrefix
The strconv.ParseFloat function parses a floating-point value from a string, but it requires that the floating-point value is the entire string, otherwise it returns an error. In many types of pars... More on github.com
🌐 github.com
10
June 12, 2022
🌐
Go Packages
pkg.go.dev › strconv
strconv package - strconv - Go Packages
ParseFloat accepts decimal and hexadecimal floating-point numbers as defined by the Go syntax for floating-point literals. If s is well-formed and near a valid floating-point number, ParseFloat returns the nearest floating-point number rounded using IEEE754 unbiased rounding.
🌐
YourBasic
yourbasic.org › golang › convert-string-to-float
Convert between float and string · YourBasic Go
yourbasic.org/golang · ... 152092096 · Use the strconv.ParseFloat function to parse a string as a floating-point number with the precision specified by bitSize: 32 for float32, or 64 for float64....
🌐
ZetCode
zetcode.com › golang › strconv-parsefloat
Using strconv.ParseFloat in Go
April 20, 2025 - Learn how to parse floating-point numbers from strings using strconv.ParseFloat in Go. Includes practical examples and error handling.
🌐
GitHub
gist.github.com › yyscamper › 5657c360fadd6701580f3c0bcca9f63a
An advance ParseFloat for golang, support scientific notation, comma separated number · GitHub
December 13, 2020 - An advance ParseFloat for golang, support scientific notation, comma separated number - parseFloat.go
🌐
Educative
educative.io › answers › how-to-convert-a-string-to-a-float-in-golang
How to convert a string to a float in Golang
A string can be converted to a float in Golang using the ParseFloat function within the strconv package.
🌐
Reddit
reddit.com › r/golang › strconv.parsefloat() faster altrernatives
r/golang on Reddit: strconv.ParseFloat() faster altrernatives
October 4, 2022 - You seem to know what you're doing in terms of parsing and profiling. Maybe optimize the ParseFloat implementation. You say you're only accepting a fixed format and I assume a fixed precision, and in the parsing process you're presumably already checking the syntax so you only need the conversion part.
Find elsewhere
🌐
IncludeHelp
includehelp.com › golang › strconv-parsefloat-function-with-examples.aspx
Golang strconv.ParseFloat() Function with Examples
September 10, 2021 - The return type of the ParseFloat() function is (float64, error), it returns the floating-point number converted from the given string. // Golang program to demonstrate the // example of strconv.ParseFloat() Function package main import ( "fmt" "strconv" ) func main() { fmt.Println(strconv.ParseFloat("123.50", 32)) fmt.Println(strconv.ParseFloat("123.50", 64)) fmt.Println(strconv.ParseFloat("-123456789.501234", 32)) fmt.Println(strconv.ParseFloat("-123456789.501234", 64)) fmt.Println() fmt.Println(strconv.ParseFloat("NaN", 32)) fmt.Println(strconv.ParseFloat("Inf", 32)) fmt.Println(strconv.ParseFloat("-Inf", 32)) }
🌐
Cloudhadoop
cloudhadoop.com › home
Golang Example - strconv ParseFloat function guide
December 31, 2023 - package main import ( "fmt" "reflect" "strconv" ) func main() { floatNumb, err: = strconv.ParseFloat("123.23", 32) fmt.Println(floatNumb) fmt.Println(reflect.TypeOf(floatNumb)) fmt.Println(err) float32Value: = float32(floatNumb) // Convert to float 32 fmt.Println(float32Value) fmt.Println(reflect.TypeOf(float32Value)) } Output: 123.2300033569336 float64 <nil> 123.23 float32 · #Golang · #Golang-examples · Categories · Javascript (6) Angular (84) Dart (116) Golang (25) Java (69) Primeng (14) Python (44) Typescript (69) Vuejs (18) Blockchain (1) Reactjs (63) Git (5) Nodejs (64) Node (1) Swift (48) Tags ·
🌐
TutorialKart
tutorialkart.com › golang-tutorial › golang-convert-string-to-float
How to convert String to Float in Go Language?
July 1, 2021 - package main import ( "fmt" "strconv" ) func main() { var str = "14.2356" result, err := strconv.ParseFloat(str, 32) if err == nil { fmt.Println("The float value is :", result) } else { fmt.Println("There is an error converting string to float.") } }
🌐
Reintech
reintech.io › blog › introduction-to-gos-strconv-package-string-conversions
An Introduction to Go's `strconv` Package: String Conversions | Reintech media
January 26, 2026 - The strconv.ParseFloat() function in Go is used to convert a string to a floating-point number. This function takes two arguments: the string to be converted and the number of bits the floating-point number should occupy (32 or 64).
🌐
TutorialsPoint
tutorialspoint.com › how-to-convert-string-to-float-type-in-golang
How to Convert string to float type in Golang?
May 5, 2023 - In Go, we can convert a string to a float type using the strconv package. The strconv package provides the ParseFloat function to convert a string to a float type. This function takes three arguments - the string to be converted, the bit size of the
Top answer
1 of 3
1

Go uses IEEE-754 binary floating-point numbers. Floating-point numbers are imprecise. Don't use them for financial transactions. Use integers.

For example,

package main

import (
    "fmt"
    "strconv"
    "strings"
)

func parseCents(s string) (int64, error) {
    n := strings.SplitN(s, ".", 3)
    if len(n) != 2 || len(n[1]) != 2 {
        err := fmt.Errorf("format error: %s", s)
        return 0, err
    }
    d, err := strconv.ParseInt(n[0], 10, 56)
    if err != nil {
        return 0, err
    }
    c, err := strconv.ParseUint(n[1], 10, 8)
    if err != nil {
        return 0, err
    }
    if d < 0 {
        c = -c
    }
    return d*100 + int64(c), nil
}

func main() {
    s := "79.35"
    fmt.Println(parseCents(s))
    s = "149.20"
    fmt.Println(parseCents(s))
    s = "-149.20"
    fmt.Println(parseCents(s))
    s = "149.2"
    fmt.Println(parseCents(s))
}

Playground: https://play.golang.org/p/mGuO51QWyIv

Output:

7935 <nil>
14920 <nil>
-14920 <nil>
0 format error: 149.2
2 of 3
0

Based on @peterSO's answer, with some bugfix and enhancement:

https://play.golang.org/p/YcRLeEJ7lTA

package main

import (
    "fmt"
    "strconv"
    "strings"
)

func parseCents(s string) (int64, error) {
    var ds string
    var cs string

    n := strings.SplitN(s, ".", 3)
    switch len(n) {
    case 1:
        ds = n[0]
        cs = "0"
    case 2:
        ds = n[0]
        switch len(n[1]) {
        case 1:
            cs = n[1] + "0"
        case 2:
            cs = n[1]
        default:
            return 0, fmt.Errorf("invalid format:%s", s)
        }
    default:
        return 0, fmt.Errorf("invalid format:%s", s)
    }

    d, err := strconv.ParseInt(ds, 10, 0)
    if err != nil {
        return 0, err
    }

    c, err := strconv.ParseUint(cs, 10, 0)
    if err != nil {
        return 0, err
    }

    cents := d * 100

    if strings.HasPrefix(s, "-") {
        cents -= int64(c)
    } else {
        cents += int64(c)
    }

    return cents, nil
}

func main() {
    examples := map[string]int64{
        "79.35": 7935,
        "149.20": 14920,
        "-149.20": -14920,
        "149.2": 14920,
        "-0.12": -12,
        "12": 1200,
        "1.234": 0,
        "1.2.34": 0,
    }

    for s, v := range examples {
        cents, err := parseCents(s)
        fmt.Println(cents, cents == v, err)
    }
}
🌐
GitHub
github.com › golang › go › issues › 3197
strconv: no way to ParseFloat / ParseInt from []byte · Issue #3197 · golang/go
January 20, 2020 - The strconv package's ParseFloat / ParseInt only take string. To reduce allocations, I need to parse an int from a []byte. I attempted to convert strconv's Parse internals to use []byte ins...
Author   bradfitz
🌐
Ramesh Fadatare
rameshfadatare.com › home › golang strconv.parsefloat function
Golang strconv.ParseFloat Function
August 11, 2024 - Difference between HashSet and ... strconv.ParseFloat function in Golang is part of the strconv package and is used to parse a string representation of a floating-point number into a float32 or float64 type....
🌐
GitHub
github.com › golang › go › issues › 53340
proposal: strconv: add ParseFloatPrefix · Issue #53340 · golang/go
June 12, 2022 - The strconv.ParseFloat function parses a floating-point value from a string, but it requires that the floating-point value is the entire string, otherwise it returns an error. In many types of parsing you want to know if a string starts ...
Author   benhoyt
🌐
GeeksforGeeks
geeksforgeeks.org › go language › how-to-convert-string-to-float-type-in-golang
How to Convert string to float type in Golang? - GeeksforGeeks
May 19, 2020 - If a1 or a2 is well-formed and near a valid floating-point number, ParseFloat returns the nearest floating-point number rounded using IEEE754 unbiased rounding which is parsing a hexadecimal floating-point value only rounds when there are more bits in the hexadecimal representation than will fit in the mantissa. ... // Golang program to Convert // string to float type package main import ( "fmt" "strconv" ) func main() { // defining a string a1 a1 := "-2.514" // converting the string a1 // into float and storing it // in b1 using ParseFloat b1, _ := strconv.ParseFloat(a1, 8) // printing the float b1 fmt.Println(b1) a2 := "-2.514" b2, _ := strconv.ParseFloat(a2, 32) fmt.Println(b2) fmt.Println(b1 + b2) } Output:
🌐
GitHub
github.com › golang › go › issues › 36657
strconv: inaccurate string to float64 conversion ParseFloat · Issue #36657 · golang/go
January 20, 2020 - strconv.ParseFloat(...) actually returns 1090544144181609278303144771584. The difference between string value and returned float64 value is 70531932370606 (see https://www.wolframalpha.com/input/?i=1090544144181609348835077142190-1090544144181609278303144771584) Go compiler correctly converts float64 literals to float64 constants, as you can see in Playground example https://play.golang.org/p/izkaZ-XBog5
Author   g7r
🌐
Go by Example
gobyexample.com › number-parsing
Go by Example: Number Parsing
Parsing numbers from strings is a basic but common task in many programs; here’s how to do it in Go · The built-in package strconv provides the number parsing