How to Use strconv.FormatComplex() Function in Golang

Golang strconv.FormatComplex() function is “used to convert the complex number c to a string of the form (a+bi) where a and b are the real and imaginary parts, formatted according to the format fmt and precision prec”.

Syntax

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

Parameters

c: It is a complex number to be converted into the string form.

fmt: It is a byte value to define the format:

  1. ‘b’ (-ddddp±ddd, a binary exponent),
  2. ‘e’ (-d.dddde±dd, a decimal exponent),
  3. ‘E’ (-d.ddddE±dd, a decimal exponent),
  4. ‘f’ (-ddd.dddd, no exponent),
  5. ‘g’ (‘e’ for large exponents, ‘f’ otherwise),
  6. ‘G’ (‘E’ for large exponents, ‘f’ otherwise),
  7. ‘x’ (-0xd.ddddp±ddd, a hexadecimal fraction and binary exponent), or
  8. ‘X’ (-0Xd.ddddP±ddd, a hexadecimal fraction and binary exponent).

prec: The precision value specifies the number of digits printed by the ‘e’, ‘E’, ‘f’, ‘g’, ‘G’, ‘x’, and ‘X’ formats.

bitSize: An integer value that specifies the bitSize bits—64 for the complex64 and 128 for the complex128.

Return value

It returns the given complex number in the string format (a+bi).

Example 1: How to Use strconv.FormatComplex() Function

package main

import (
  "fmt"
  "strconv"
)

func main() {
  fmt.Println(strconv.FormatComplex(complex(19, 21), 'k', 0, 64))
  fmt.Println(strconv.FormatComplex(19+21i, 'f', 0, 64))
  fmt.Println(strconv.FormatComplex(complex(19, 21), 'f', 0, 128))
  fmt.Println(strconv.FormatComplex(19+21i, 'f', 0, 128))
  fmt.Println(strconv.FormatComplex(complex(2.1, 1.19), 'f', 2, 128))
  fmt.Println(strconv.FormatComplex(2.1+19i, 'f', 2, 128))
  fmt.Println(strconv.FormatComplex(2.1+319i, 'E', -1, 128))
}

Output

(%k+%ki)
(19+21i)
(19+21i)
(19+21i)
(2.10+1.19i)
(2.10+19.00i)
(2.1E+00+3.19E+02i)

Example 2: Complex code of strconv.FormatComplex() Function

package main

import (
  "fmt"
  "strconv"
)

func main() {
  // Initialize complex numbers
  c1 := complex(2, 3)
  c2 := complex(4, -5)

  // Do some complex number operations
  sum := c1 + c2
  product := c1 * c2
  quotient := c1 / c2

  // Convert results to strings using strconv.FormatComplex()
  sumStr := strconv.FormatComplex(sum, 'f', 6, 64)
  productStr := strconv.FormatComplex(product, 'f', 6, 64)
  quotientStr := strconv.FormatComplex(quotient, 'f', 6, 64)

  // Print results
  fmt.Printf("Sum of %s and %s: %s\n", complexToString(c1), 
              complexToString(c2), sumStr)
  fmt.Printf("Product of %s and %s: %s\n", complexToString(c1), 
              complexToString(c2), productStr)
  fmt.Printf("Quotient of %s and %s: %s\n", complexToString(c1), 
              complexToString(c2), quotientStr)
}
func complexToString(c complex128) string {
  return fmt.Sprintf("%g + %gi", real(c), imag(c))
}

Output

Sum of 2 + 3i and 4 + -5i: (6.000000-2.000000i)
Product of 2 + 3i and 4 + -5i: (23.000000+2.000000i)
Quotient of 2 + 3i and 4 + -5i: (-0.170732+0.536585i)

That’s it.

Leave a Comment