ADVERTISEMENT
ADVERTISEMENT

Golang strconv.ParseComplex() Function with Examples

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

strconv.ParseComplex()

The ParseComplex() function is an inbuilt function of the strconv package which is used to convert the given string s to a complex number with the precision specified by bitSize (Value of bitSize must be 64 or 128, 64 is used for complex64, and 128 is used for complex128). When bitSize=64, the converted result still has type complex128, but it will be convertible to complex64 without changing its value.

It accepts two parameters (str, bitSize) and returns the complex number converted from the given string.

Syntax:

func ParseComplex(s string, bitSize int) (complex128, error)

Parameter(s):

  • s : String value which is to be parsed in the complex number.
  • bitSize : To define the precision, the value can be 64 or 128.

Return Value:

The return type of ParseComplex() function is (complex128, error), it returns the complex number converted from the given string.

Example 1:

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

package main

import (
	"fmt"
	"strconv"
)

func main() {
	fmt.Println(strconv.ParseComplex("2+3i", 64))
	fmt.Println(strconv.ParseComplex("2+3i", 128))
	fmt.Println(strconv.ParseComplex("2.56+3.192i", 64))
	fmt.Println(strconv.ParseComplex("2.56+3.192i", 128))
	fmt.Println()

	fmt.Println(strconv.ParseComplex("2-3i", 64))
	fmt.Println(strconv.ParseComplex("2-3i", 128))
	fmt.Println(strconv.ParseComplex("-2.56+3.192i", 64))
	fmt.Println(strconv.ParseComplex("-2.56+3.192i", 128))
}

Output:

(2+3i) <nil>
(2+3i) <nil>
(2.559999942779541+3.191999912261963i) <nil>
(2.56+3.192i) <nil>

(2-3i) <nil>
(2-3i) <nil>
(-2.559999942779541+3.191999912261963i) <nil>
(-2.56+3.192i) <nil>

Example 2:

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

package main

import (
	"fmt"
	"strconv"
)

func main() {
	v := "2+3i"

	s, err := strconv.ParseComplex(v, 64)
	if err == nil {
		fmt.Println("Parsing done...")
		fmt.Printf("%T, %v\n", s, s)
	} else {
		fmt.Println("Parsing failed...")
		fmt.Printf("Error:%v\n", err)
	}
	fmt.Println()

	v = "-2.5-4.5i"
	s, err = strconv.ParseComplex(v, 64)
	if err == nil {
		fmt.Println("Parsing done...")
		fmt.Printf("%T, %v\n", s, s)
	} else {
		fmt.Println("Parsing failed...")
		fmt.Printf("Error:%v\n", err)
	}
	fmt.Println()

	v = "2.5+36i"
	s, err = strconv.ParseComplex(v, 128)
	if err == nil {
		fmt.Println("Parsing done...")
		fmt.Printf("%T, %v\n", s, s)
	} else {
		fmt.Println("Parsing failed...")
		fmt.Printf("Error:%v\n", err)
	}
	fmt.Println()

	v = "2.5 + 36i"
	s, err = strconv.ParseComplex(v, 128)
	if err == nil {
		fmt.Println("Parsing done...")
		fmt.Printf("%T, %v\n", s, s)
	} else {
		fmt.Println("Parsing failed...")
		fmt.Printf("Error:%v\n", err)
	}
}

Output:

Parsing done...
complex128, (2+3i)

Parsing done...
complex128, (-2.5-4.5i)

Parsing done...
complex128, (2.5+36i)

Parsing failed...
Error:strconv.ParseComplex: parsing "2.5 + 36i": invalid syntax

Golang strconv Package »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.