Use strconv.FormatFloat like such:

s := strconv.FormatFloat(3.1415, 'f', -1, 64)
fmt.Println(s)

Outputs

3.1415

Answer from Ullaakut on Stack Overflow
🌐
YourBasic
yourbasic.org › golang › convert-string-to-float
Convert between float and string · YourBasic Go
CODE EXAMPLE Use strconv.ParseFloat to parse a floating point number from a string, and fmt.Sprintf to format a float as a string.
Discussions

Converting a float to string
http://golang.org/pkg/strconv/#FormatFloat http://golang.org/pkg/strconv/#ParseFloat If you don't care about errors, write a small wrapper func. More on reddit.com
🌐 r/golang
5
3
November 20, 2014
Problem converting float32 to string
HI I have problem with float32 to string conversion. My test code: package main import ( "fmt" "strconv" ) func main() { var t float32 t = 5.9902389 numeroString := strconv.FormatFloat(float64(t), 'f', -1, 32) fmt.Println("Number float32:", t) fmt.Println("Number string:", numeroString) } Responce: ... More on forum.golangbridge.org
🌐 forum.golangbridge.org
1
0
July 20, 2023
go - formatFloat : convert float number to string - Stack Overflow
How do I convert a float number into string format? This is google playground but not getting the expected output. More on stackoverflow.com
🌐 stackoverflow.com
Issue with type conversion from float64 to string
GO version- go1.13.1 when converting from float64 to string - GO is changing the value for Odd numbers. SAMPLE CODE- package main import ( "fmt" "strconv" ) func main() { var g ... More on github.com
🌐 github.com
2
January 31, 2020
🌐
Educative
educative.io › answers › how-can-we-convert-a-float-into-a-string-in-golang
How can we convert a float into a string in Golang
A float can be converted into a string, in Golang, using the following two functions: FormatFloat: This function is available within the strconv package. Sprintf: This function is available within the fmt package.
🌐
Go Packages
pkg.go.dev › strconv
strconv package - strconv - Go Packages
FormatFloat converts the floating-point number f to a string, according to the format fmt and precision prec.
🌐
Reddit
reddit.com › r/golang › converting a float to string
r/golang on Reddit: Converting a float to string
November 20, 2014 -

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

🌐
Go Forum
forum.golangbridge.org › getting help
Problem converting float32 to string - Getting Help - Go Forum
July 20, 2023 - My test code: package main import ( "fmt" "strconv" ) func main() { var t float32 t = 5.9902389 numeroString := strconv.FormatFloat(float64(t), 'f', -1, 32) fmt.Println("Number float32:", t) fmt.Println("Number string:", numeroString) } Responce: ...
🌐
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 - It takes three arguments: the floating-point number, the format ('f', 'b', 'e', or 'g'), and the precision (number of decimal points), and the number of bits the floating-point number occupies (32 or 64). It returns a string representation of the floating-point number. The strconv.Itoa() function in Go is used to convert an integer to a string.
🌐
GitHub
gist.github.com › morrxy › b5972d4516e1e41229aba77f85894942
[int float to string] #golang · GitHub
[int float to string] #golang · Raw · float2str.go · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Find elsewhere
🌐
Admfactory
admfactory.com › home › how to convert a float to string in golang
How to convert a float to string in Golang | ADMFactory
June 11, 2020 - package main import ( "fmt" "strconv" ) func main() { /** float variable */ f := 1.23456789 /** converting the f variable into a string */ /** 5 is the number of decimals */ /** 64 is for float64 type*/ str := strconv.FormatFloat(f, 'f', 5, 64) /** displaying the string variable into the console */ fmt.Println(str) } To compile the code navigate to the file location and run the following command.
🌐
Educative
educative.io › answers › how-to-convert-a-string-to-a-float-in-golang
How to convert a string to a float in Golang
If the input string is non-numeric, then this function will throw an invalid syntax error and the float value will be set to 0. The code snippet below shows how to convert a string to a float using the ParseFloat function.
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-convert-data-types-in-go
How To Convert Data Types in Go | DigitalOcean
May 9, 2019 - Strings can be converted to numbers by using the strconv package in the Go standard library. The strconv package has functions for converting both integer and float number types. This is a very common operation when accepting input from the user. For example, if you had a program that asked ...
🌐
TutorialKart
tutorialkart.com › golang-tutorial › golang-convert-string-to-float
How to convert String to Float in Go Language?
July 1, 2021 - In this tutorial, we will learn the syntax of ParseFloat() function, and how to use this function to parse or convert a string to float value.
🌐
Python Examples
pythonexamples.org › go › how-to-convert-float-to-string
How to convert Float to String in Go
package main import ( "fmt" "strconv" ... declare a float variable float2 with a value of 10.5. We import the strconv package. We use the FormatFloat function to convert the float to a string and store the result in a variable str2....
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-convert-string-to-float-type-in-golang
How to Convert string to float type in Golang? - GeeksforGeeks
May 19, 2020 - In order to convert Boolean Type to String type in Golang , you can use the strconv and fmt package function. 1. strconv.FormatBool() Method: The FormatBool is used to Boolean Type to String.
🌐
Siongui
siongui.github.io › 2017 › 04 › 13 › go-string-float-number-type-casting
[Golang] Type Conversion between String and Floating Number
package main import ( "fmt" "strconv" ) func main() { width := "560" height := "315" // convert string to float64 w, err := strconv.ParseFloat(width, 64) if err != nil { fmt.Println(err) } h, err := strconv.ParseFloat(height, 64) if err != nil { fmt.Println(err) } // make calculation and then convert float64 to string aspectRatio := strconv.FormatFloat(h*100/w, 'f', -1, 64) + "%" fmt.Println("aspect ratio: ", aspectRatio) }
🌐
Go
go.dev › src › math › big › floatconv.go
This file implements string-to-Float conversion functions.
259 func (z *Float) Parse(s string, base int) (f *Float, b int, err error) { 260 // scan doesn't handle ±Inf 261 if len(s) == 3 && (s == "Inf" || s == "inf") { 262 f = z.SetInf(false) 263 return 264 } 265 if len(s) == 4 && (s[0] == '+' || s[0] == '-') && (s[1:] == "Inf" || s[1:] == "inf") { 266 f = z.SetInf(s[0] == '-') 267 return 268 } 269 270 r := strings.NewReader(s) 271 if f, b, err = z.scan(r, base); err != nil { 272 return 273 } 274 275 // entire string must have been consumed 276 if ch, err2 := r.ReadByte(); err2 == nil { 277 err = fmt.Errorf("expected end of string, found %q", ch) 278
🌐
GitHub
github.com › golang › go › issues › 36922
Issue with type conversion from float64 to string · Issue #36922 · golang/go
January 31, 2020 - package main import ( "fmt" "strconv" ) func main() { var g float64 g = 12643612166373552 var ss string ss = strconv.FormatFloat(g, 'f', 0, 64) fmt.Println(ss)
Author   neetikh
Top answer
1 of 2
29

Both fmt.Sprintf and strconv.FormatFloat use the same string formatting routine under the covers, so should give the same results.

If the precision that the number should be formatted to is variable, then it is probably easier to use FormatFloat, since it avoids the need to construct a format string as you would with Sprintf. If it never changes, then you could use either.

The last argument to FormatFloat controls how values are rounded. From the documentation:

It rounds the result assuming that the original was obtained from a floating-point value of bitSize bits (32 for float32, 64 for float64)

So if you are working with float32 values as in your sample code, then passing 32 is correct.

2 of 2
5

You will have with Go 1.12 (February 2019) and the project cespare/ryu a faster alternative to strconv:

Ryu is a Go implementation of Ryu, a fast algorithm for converting floating-point numbers to strings.
It is a fairly direct Go translation of Ulf Adams's C library.

The strconv.FormatFloat latency is bimodal because of an infrequently-taken slow path that is orders of magnitude more expensive (issue 15672).

The Ryu algorithm requires several lookup tables.
Ulf Adams's C library implements a size optimization (RYU_OPTIMIZE_SIZE) which greatly reduces the size of the float64 tables in exchange for a little more CPU cost.

For a small fraction of inputs, Ryu gives a different value than strconv does for the last digit.
This is due to a bug in strconv: issue 29491.

Go 1.12 might or might not include that new implementation directly in strconv, but if it does not, you can use this project for faster conversion.