Use strconv.FormatFloat like such:
s := strconv.FormatFloat(3.1415, 'f', -1, 64)
fmt.Println(s)
Outputs
Answer from Ullaakut on Stack Overflow
3.1415
Use strconv.FormatFloat like such:
s := strconv.FormatFloat(3.1415, 'f', -1, 64)
fmt.Println(s)
Outputs
3.1415
Convert float to 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).
func FormatFloat(f float64, fmt byte, prec, bitSize int) string
f := 3.14159265
s := strconv.FormatFloat(f, 'E', -1, 64)
fmt.Println(s)
Output is "3.14159265"
Another method is by using fmt.Sprintf
s := fmt.Sprintf("%f", 123.456)
fmt.Println(s)
Output is "123.456000"
Check the code on play ground
Convert float to string in Go lang as per the required format - Stack Overflow
Problem converting float32 to string
How to format floating point numbers into a string using Go - Stack Overflow
go - formatFloat : convert float number to string - Stack Overflow
Package strconv
import "strconv" > func FormatFloat
func FormatFloat(f float64, fmt byte, prec, bitSize int) stringFormatFloat 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
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
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.
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.FormatFloatlatency 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 thefloat64tables in exchange for a little more CPU cost.For a small fraction of inputs, Ryu gives a different value than
strconvdoes for the last digit.
This is due to a bug instrconv: 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.
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
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
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
I think you are just using the wrong format specifier:
package main
import (
"fmt"
"strconv"
)
func main() {
fmt.Println(strconv.FormatFloat(-3.6739403974420544e-15, 'e', -1, 64))
}
Output:
-3.6739403974420544e-15
The docs show the other formats as well.
This should do it
f := -3.6739403974420544e-15
fmt.Println(fmt.Sprint(f))