Golang strconv.FormatFloat() function example
package strconv
Golang strconv.FormatFloat() function usage example
package main
import (
"fmt"
"strconv"
"math/rand"
)
func main() {
//func FormatFloat(f float64, fmt byte, prec, bitSize int) string
x := rand.NormFloat64()
str := strconv.FormatFloat(x, 'g', -1, 64)
fmt.Println(str)
}
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.
Reference :
Advertisement
Something interesting
Tutorials
+4.7k Unix/Linux : How to pipe/save output of a command to file?
+5.1k Swift : Convert (cast) Float to Int or Int32 value
+9.9k Golang : ffmpeg with os/exec.Command() returns non-zero status
+8.2k Golang : Find relative luminance or color brightness
+22.3k Golang : Read directory content with filepath.Walk()
+8.1k Golang : How To Use Panic and Recover
+6.7k Golang : When to use make or new?
+21.3k Golang : Create and resolve(read) symbolic links
+17k Golang : Covert map/slice/array to JSON or XML format
+6k Golang : Convert Chinese UTF8 characters to Pin Yin
+7.5k Golang : Detect sample rate, channels or latency with PortAudio
+5k Golang : Calculate a pip value and distance to target profit example