Golang strconv.FormatComplex() Function with Examples

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

strconv.FormatComplex()

The FormatComplex() function is an inbuilt function of the strconv package which is used to convert the given complex number c to a string of the form (a+bi) where a is the real part and b is the imaginary part of the complex number, the formatting is done based on the format fmt and precision prec.

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

Syntax:

func FormatComplex(
    c complex128, 
    fmt byte, 
    prec, bitSize int) string

Parameter(s):

  • c : The complex 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 (64 for the complex64 and 128 for the complex128).

Return Value:

The return type of FormatComplex() function is a string, it returns the given complex number in the string format (of the form (a+bi)).

Example 1:

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

package main

import (
	"fmt"
	"strconv"
)

func main() {
	fmt.Println(strconv.FormatComplex(complex(2, 3), 'f', 0, 64))
	fmt.Println(strconv.FormatComplex(2+3i, 'f', 0, 64))
	fmt.Println(strconv.FormatComplex(complex(2, 3), 'f', 0, 128))
	fmt.Println(strconv.FormatComplex(2+3i, 'f', 0, 128))
	fmt.Println(strconv.FormatComplex(complex(2.5, 3.19), 'f', 2, 128))
	fmt.Println(strconv.FormatComplex(2.5+319i, 'f', 2, 128))
	fmt.Println(strconv.FormatComplex(2.5+319i, 'E', -1, 128))
}

Output:

(2+3i)
(2+3i)
(2+3i)
(2+3i)
(2.50+3.19i)
(2.50+319.00i)
(2.5E+00+3.19E+02i)

Example 2:

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

package main

import (
	"fmt"
	"strconv"
)

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

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

	y = complex(2.5, 3.19)
	result = strconv.FormatComplex(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.FormatComplex(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 complex128, value (2+3i)
result: Type string, value "(2+3i)"

y: Type complex128, value (2.5+3.19i)
result: Type string, value "(2.50+3.19i)"

z: Type complex128, value (4.5+6.1899999999999995i)
result: Type string, value "(4.50+6.19i)"

Golang strconv Package »




Comments and Discussions!

Load comments ↻





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