Monday, December 10, 2018

Golang Example - strconv ParseFloat function guide

This blog post covers package strconv ParseFloat Function with examples in the Go Language.
Golang Example - strconv ParseFloat function guide

golang strconv ParseFloat function

strconv is a standard inbuilt package in go language that provides various function implementations for converting a string to types into an int, float, boolean etc..
String to float is a common task of a programmer during development
ParseFloat is an inbuilt function, converts a String value to a floating  number
You can check official documentation is here
Here is the syntax of this function
func ParseFloat(inputstring string, bitSize int) (float64, error)
Argument list
inputstring is a numeric floating number represented in string bitSize is int type argument of precision value. It can be 32 for float32 or 64 for float64
Return type 
This function always returns two values. The float64 value is returned which contains a floating number.we can convert to float32 value if required. the error is returned with *NumError value if unable to convert a string to a floating number

function usage - success and error cases 

Following are various uses cases of converting a string to floating types In the below code, ParseFloat() function takes a string and convert it to a floating number with given precision of 64. For Conversion, 64bits are considered. Go language uses IEEE_754  binary format for storing floating numbers Returned floating number of type float64 and error-nil is returned.
floatNumb, err := strconv.ParseFloat("123.3412312312", 64)
fmt.Println(floatNumb)
fmt.Println(reflect.TypeOf(floatNumb))
fmt.Println(err)
Output is
123.3412312312
float64
<nil>
Now we will see changing precision value from 64 to 32
floatNumb, err := strconv.ParseFloat("123.3412312312", 32)
 fmt.Println(floatNumb)
 fmt.Println(reflect.TypeOf(floatNumb))
 fmt.Println(err)
 
output is
123.34123229980469
float64
<nil>
The below example gives an error - strconv.ParseFloat: parsing "abc": invalid syntax when given input string is not numeric In this case, this function returns 0 value of type float64 and err contains a description of an error
floatNumb, err := strconv.ParseFloat("abc", 32)
 fmt.Println(floatNumb)
 fmt.Println(reflect.TypeOf(floatNumb))
 fmt.Println(err)
 
Output is
0
float64
strconv.ParseFloat: parsing "abc": invalid syntax

ParseFloat function Example program

Here is an example for Converting string to float64 numbers using parseFloat.
package main

import (
 "fmt"
 "reflect"
 "strconv"
)

func main() {
 floatString := "12545.23"
 floatNumb, err := strconv.ParseFloat("abc", 32)
 fmt.Println(floatNumb)
 fmt.Println(reflect.TypeOf(floatNumb))
 fmt.Println(err)
 if n1, err := strconv.ParseFloat(floatString, 32); err == nil {
  fmt.Printf("%T, %v\n", n1, n1)
 }
 if n2, err := strconv.ParseFloat(floatString, 64); err == nil {
  fmt.Printf("%T, %v\n", n2, n2)
 }

}
Output is
0
float64
strconv.ParseFloat: parsing "abc": invalid syntax
float64, 12545.23046875
float64, 12545.23

Convert String to float64/float32 values

ParseFloat() function returns float64 value by default. float32(float64value) function used to convert to float32 value Here is an program code for casting string to float64/float32
package main

import (
 "fmt"
 "reflect"
 "strconv"
)

func main() {
 floatNumb, err := strconv.ParseFloat("123.23", 32)
 fmt.Println(floatNumb)
 fmt.Println(reflect.TypeOf(floatNumb))
 fmt.Println(err)
 float32Value := float32(floatNumb) // Convert to float 32
 fmt.Println(float32Value)
 fmt.Println(reflect.TypeOf(float32Value))
}
Output is
123.2300033569336
float64
<nil>
123.23
float32

Related article


EmoticonEmoticon