Standard library / strconv

strconv.FormatFloat()

Formats a float with format byte (such as f, e, g, x), precision and bitSize 32 or 64. Precision -1 uses the shortest representation that round-trips for the chosen size; f precision gives decimal places. Expected stdout: '1.250\n'.

Formal Syntax & Structure

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

Parameters & Attributes

NameTypeDescription
ffloat64Floating-point value to encode.
fmtbyteFormatting verb byte such as f, e or g.
precintVerb-dependent output precision; -1 selects round-trip precision.
bitSizeintFloating-point precision width, 32 or 64.

Return Value

string

Encoded representation.

Working Example

Example Snippetgo
package main
import (  "fmt"  "strconv")
func main() {  fmt.Println(strconv.FormatFloat(1.25, 'f', 3, 64))}
Go Reference