Golang strconv.FormatFloat() Function with Examples

Golang | strconv.FormatFloat() Function: Here, we are going to learn about the FormatFloat() function of the strconv package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 07, 2021

strconv.FormatFloat()

The FormatFloat() function is an inbuilt function of the strconv package which is used to convert the given floating-point number f to a string, the formatting is done based on the format fmt and precision prec. The function rounds the result assuming that the original was obtained from a floating-point value of bitSize bits (where, 32 for float32, 64 for float64).

It accepts four parameters (f, fmt, prec, bitSize) and returns the given complex number in the string format (of the form (a+bi)).

Syntax:

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

Parameter(s):

  • f : The floating-point number which is to be converted in the string form.
  • fmt : A byte value to define the format:
    • '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),
    • 'G' ('E' for large exponents, 'f' otherwise),
    • 'x' (-0xd.ddddp±ddd, a hexadecimal fraction and binary exponent), or
    • 'X' (-0Xd.ddddP±ddd, a hexadecimal fraction and binary exponent).
  • prec : The value to define the precision that controls the number of digits (excluding the exponent) printed by the 'e', 'E', 'f', 'g', 'G', 'x', and 'X' formats.
    • For the format value ('e', 'E', 'f', 'x', and 'X') – prec is the number of digits after the decimal point.
    • And, For the format value ('g' and 'G') – prec is the maximum number of significant digits (trailing zeros are removed).
    • The special precision value (-1) is used for the smallest number of digits necessary such that ParseFloat() will return f exactly.
  • bitSize : An integer value to define the bitSize bits (32 for float32, 64 for float64).

Return Value:

The return type of FormatFloat() function is a string, it returns the given floating-point in the string format.

Example 1:

// Golang program to demonstrate the
// example of strconv.FormatFloat() Function

package main

import (
	"fmt"
	"strconv"
)

func main() {
	fmt.Println(strconv.FormatFloat(123.45, 'f', 2, 32))
	fmt.Println(strconv.FormatFloat(-123.45, 'f', 2, 32))
	fmt.Println(strconv.FormatFloat(123.45, 'f', 2, 64))
	fmt.Println(strconv.FormatFloat(-123.45, 'f', 2, 64))
	fmt.Println()

	fmt.Println(strconv.FormatFloat(123.45, 'E', 2, 64))
	fmt.Println(strconv.FormatFloat(-123.45, 'E', -1, 64))
}

Output:

123.45
-123.45
123.45
-123.45

1.23E+02
-1.2345E+02

Example 2:

// Golang program to demonstrate the
// example of strconv.FormatFloat() Function

package main

import (
	"fmt"
	"strconv"
)

func main() {
	var x float64
	var y float64
	var result string

	x = 123.45
	result = strconv.FormatFloat(x, 'f', 2, 64)
	fmt.Printf("x: Type %T, value %v\n", x, x)
	fmt.Printf("result: Type %T, value %q\n", result, result)
	fmt.Println()

	y = 64.856
	result = strconv.FormatFloat(y, 'f', 2, 64)
	fmt.Printf("y: Type %T, value %v\n", y, y)
	fmt.Printf("result: Type %T, value %q\n", result, result)
	fmt.Println()

	z := x + y
	result = strconv.FormatFloat(z, 'f', 2, 64)
	fmt.Printf("z: Type %T, value %v\n", z, z)
	fmt.Printf("result: Type %T, value %q\n", result, result)
}

Output:

x: Type float64, value 123.45
result: Type string, value "123.45"

y: Type float64, value 64.856
result: Type string, value "64.86"

z: Type float64, value 188.30599999999998
result: Type string, value "188.31"

Example 3:

// Golang program to demonstrate the
// example of strconv.FormatFloat() Function

package main

import (
	"fmt"
	"strconv"
)

func FloatToString(num float64) string {
	// to convert a float number to a string
	return strconv.FormatFloat(num, 'f', 3, 64)
}

func main() {
	fmt.Println(FloatToString(123456.654321))
	fmt.Println(FloatToString(-1123456.654678))
}

Output:

123456.654
-1123456.655

Example 4:

// Golang program to demonstrate the
// example of strconv.FormatFloat() Function

package main

import (
	"fmt"
	"strconv"
)

func main() {
	var (
		x float64 = 12.345
		y float64 = 123456.654321
		z float64 = 108.9
	)

	result1 := strconv.FormatFloat(x, 'b', -1, 32)
	fmt.Printf("%T, %v\n", result1, result1)

	result2 := strconv.FormatFloat(x, 'e', -1, 64)
	fmt.Printf("%T, %v\n", result2, result2)

	result3 := strconv.FormatFloat(x, 'f', -1, 64)
	fmt.Printf("%T, %v\n", result3, result3)

	result4 := strconv.FormatFloat(y, 'g', -1, 64)
	fmt.Printf("%T, %v\n", result4, result4)

	result5 := strconv.FormatFloat(z, 'g', -1, 64)
	fmt.Printf("%T, %v\n", result5, result5)
}

Output:

string, 12944671p-20
string, 1.2345e+01
string, 12.345
string, 123456.654321
string, 108.9

Golang strconv Package »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.