ADVERTISEMENT
ADVERTISEMENT

Golang strconv.ParseFloat() Function with Examples

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

strconv.ParseFloat()

The ParseFloat() function is an inbuilt function of the strconv package which is used to convert the given string s to a floating-point number with the precision specified by bitSize (Value of bitSize must be 32 or 64, 32 is used for float32, or 64 is used for float64). When bitSize=32, the converted result still has type float64, but it will be convertible to float32 without changing its value.

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

Syntax:

func ParseFloat(s string, bitSize int) (float64, error)

Parameter(s):

  • s : String value which is to be parsed in the floating-pointing number.
  • bitSize : To define the precision, the value can be 32 or 64.

Return Value:

The return type of the ParseFloat() function is (float64, error), it returns the floating-point number converted from the given string.

Example 1:

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

package main

import (
	"fmt"
	"strconv"
)

func main() {
	fmt.Println(strconv.ParseFloat("123.50", 32))
	fmt.Println(strconv.ParseFloat("123.50", 64))
	fmt.Println(strconv.ParseFloat("-123456789.501234", 32))
	fmt.Println(strconv.ParseFloat("-123456789.501234", 64))
	fmt.Println()

	fmt.Println(strconv.ParseFloat("NaN", 32))
	fmt.Println(strconv.ParseFloat("Inf", 32))
	fmt.Println(strconv.ParseFloat("-Inf", 32))
}

Output:

123.5 <nil>
123.5 <nil>
-1.23456792e+08 <nil>
-1.23456789501234e+08 <nil>

NaN <nil>
+Inf <nil>
-Inf <nil>

Example 2:

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

package main

import (
	"fmt"
	"strconv"
)

func main() {
	v := "123.45"

	s, err := strconv.ParseFloat(v, 32)
	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 = "-123456.789123"
	s, err = strconv.ParseFloat(v, 32)
	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 = "1234567890.987654321"
	s, err = strconv.ParseFloat(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 = "1.2345678909876542e+09"
	s, err = strconv.ParseFloat(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 = "123.0f"
	s, err = strconv.ParseFloat(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)
	}
}

Output:

Parsing done...
float64, 123.44999694824219

Parsing done...
float64, -123456.7890625

Parsing done...
float64, 1.2345678909876542e+09

Parsing done...
float64, 1.2345678909876542e+09

Parsing failed...
Error:strconv.ParseFloat: parsing "123.0f": 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.