How to print float as string in Golang without scientific notation

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)