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

Convert float to string in Go lang as per the required format - Stack Overflow
FormatFloat converts the floating-point number f to a string, according to the format fmt and precision prec. It rounds the result assuming that the original was obtained from a floating-point value of bitSize bits (32 for float32, 64 for float64). The format fmt is one of 'b' (-ddddp±ddd, a binary exponent), 'e' (-d.dddde±dd, a decimal ... More on stackoverflow.com
🌐 stackoverflow.com
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
How to format floating point numbers into a string using Go - Stack Overflow
Using Go I'm trying to find the "best" way to format a floating point number into a string. I've looked for examples however I cannot find anything that specifically answers the questions I have. ... More on stackoverflow.com
🌐 stackoverflow.com
go - formatFloat : convert float number to string - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 2
12

Package strconv

import "strconv" > func FormatFloat

func FormatFloat(f float64, fmt byte, prec, bitSize int) string

FormatFloat converts the floating-point number f to a string, according to the format fmt and precision prec. It rounds the result assuming that the original was obtained from a floating-point value of bitSize bits (32 for float32, 64 for float64).

The format fmt is one of 'b' (-ddddp±ddd, a binary exponent), 'e' (-d.dddde±dd, a decimal exponent), 'E' (-d.ddddE±dd, a decimal exponent), 'f' (-ddd.dddd, no exponent), 'g' ('e' for large exponents, 'f' otherwise), or 'G' ('E' for large exponents, 'f' otherwise).

The precision prec controls the number of digits (excluding the exponent) printed by the 'e', 'E', 'f', 'g', and 'G' formats. For 'e', 'E', and 'f' it is the number of digits after the decimal point. For 'g' and 'G' it is the total number of digits. The special precision -1 uses the smallest number of digits necessary such that ParseFloat will return f exactly.

Use a precision of -1, not 1. Use a format of f, not g to avoid exponent form for large exponents (see HectorJ's comment).

startLat := strconv.FormatFloat(o.Coordinate.Longitude, 'f', -1, 64)

For example,

package main

import (
    "fmt"
    "strconv"
)

func main() {
    f := 64.2345
    s := strconv.FormatFloat(f, 'g', 1, 64)
    fmt.Println(s)
    s = strconv.FormatFloat(f, 'f', -1, 64)
    fmt.Println(s)
}

Output:

6e+01
64.2345
2 of 2
0

Some other options:

package main
import "fmt"

func main() {
   n := 64.2345
   { // example 1
      s := fmt.Sprint(n)
      fmt.Println(s == "64.2345")
   }
   { // example 2
      s := fmt.Sprintf("%v", n)
      fmt.Println(s == "64.2345")
   }
   { // example 3
      s := fmt.Sprintf("%g", n)
      fmt.Println(s == "64.2345")
   }
}
  • https://golang.org/pkg/fmt#Sprint
  • https://golang.org/pkg/fmt#Sprintf
🌐
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. It rounds the result assuming that the original was obtained from a floating-point value of bitSize bits (32 for float32, 64 for float64). ... The precision prec controls the number ...
🌐
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
The code snippet below shows us how to convert a float into a string while using the FormatFloat function. ... The code snippet below shows us how to convert a float into a string, while using the Sprintf function.
🌐
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: ...
🌐
OneLinerHub
onelinerhub.com › golang › how-to-format-float-without-decimals
Golang: How to format float without decimals - OneLinerHub
package main import "fmt" func main() { res := fmt.Sprintf("Float: %.0f", 123.3) fmt.Println(res) }ctrl + c
Find elsewhere
🌐
Educative
educative.io › answers › how-to-use-the-strconvformatfloat-function-in-golang
How to use the strconv.FormatFloat() function in Golang
The FormatFloat() function in the strconv package is used to convert a given floating-point number f to a string.
🌐
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 - How to convert a float to a string in Golang using strconv.FormatFloat function.
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.

Top answer
1 of 2
44

The package doc of fmt explains it: The %v verb is the default format, which for floating numbers means / reverts to %g which is

%e for large exponents, %f otherwise. Precision is discussed below.

If you always want "decimal point but no exponent, e.g. 123.456", use %f explicitly.

But you can only use that for floating numbers, so you have to check the type of the value you print. For that you may use a type switch or type assertion.

Example:

switch v.(type) {
case float64, float32:
    fmt.Printf("%f\n", v)
default:
    fmt.Printf("%v\n", v)
}

Output (try it on the Go Playground):

mydata
1234567890.123000
2 of 2
4

You can use %f to print a float. Given your slice of interfaces, you first need to check the type of the element. You can do so as follows:

package main

import (
    "fmt"
)

func main() {
    values := []interface{}{"mydata", 1234567890.123}
    for _, v := range values {
        // Check if the type conversion to float64 succeeds.
        if f, ok := v.(float64); ok {
            fmt.Printf("%f\n", f)
        } else {
            fmt.Println(v)
        }
    }
}

Outputs:

mydata
1234567890.123000

The full list of flags for floats from fmt is:

%b  decimalless scientific notation with exponent a power of two,
    in the manner of strconv.FormatFloat with the 'b' format,
    e.g. -123456p-78
%e  scientific notation, e.g. -1.234456e+78
%E  scientific notation, e.g. -1.234456E+78
%f  decimal point but no exponent, e.g. 123.456
%F  synonym for %f
%g  %e for large exponents, %f otherwise. Precision is discussed below.
%G  %E for large exponents, %F otherwise
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-convert-data-types-in-go
How To Convert Data Types in Go | DigitalOcean
May 9, 2019 - For example, if you had a program ... math with it. If your string does not have decimal places, you’ll most likely want to convert it to an integer by using the strconv.Atoi function....
🌐
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

🌐
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.FormatFloat() function in Go is used to convert a floating-point number to a string. 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 ...
🌐
Google Groups
groups.google.com › g › golang-nuts › c › JlUWmeDtkZY
keep just 2 decimal places in a float64
Printing converts float to string is not exact. Truncating the string for display does not truncate the stored float.
🌐
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.
🌐
Medium
medium.com › @kode-n-rolla › type-conversion-in-go-a-handy-cheat-sheet-for-developers-51482d4dca0c
Type Conversion in Go: A Handy Cheat Sheet for Developers | by k0de-n-Яolla | Medium
February 27, 2026 - Go’s strings are UTF-8 encoded, so they can include multibyte characters like emojis or non-Latin scripts. Converting to runes ensures accurate handling of characters, especially for indexing or slicing. Go provides built-in support for converting between numeric types like int, float64, and others.