How to Convert String to Float in Golang

To convert a string to float in Go, you can use the “strconv.ParseFloat()” function. This function takes two arguments: the string to be parsed and the bit size (32 or 64) of the float to be returned.

If the string cannot be converted to a float (for example, if it contains non-numeric characters), the function returns an error. It’s important to always check for this error.


func ParseFloat(s string, bitSize int) (float64, error)

Visual representation

Visual representation of converting float to string in GoExample

package main

import (

func main() {
  fValue := "19.21"
  if s, err := strconv.ParseFloat(fValue, 32); err == nil {
    fmt.Printf("%T, %v\n", s, s)
  if s, err := strconv.ParseFloat(fValue, 64); err == nil {
    fmt.Printf("%T, %v\n", s, s)


float64, 19.209999084472656
float64, 19.21

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.