AppDividend
Latest Code Tutorials

How To Convert String To Float In Golang Example

0

Golang string.ParseFloat() is an inbuilt function that converts a string s to a floating-point number with a precision specified by bitSize: 32 for float32, or 64 for float64. When bitSize=32, the result still has type float64, but it will be convertible to float32 without changing its value.

Golang String To Float Example

Golang string.ParseFloat() method accepts decimal and hexadecimal floating-point number syntax. If s is well-formed and near a valid floating-point number, ParseFloat returns the nearest floating-point number rounded using IEEE754 unbiased rounding.

The errors that ParseFloat returns have particular type *NumError and include err.Num = s.

If s is not syntactically well-formed, ParseFloat returns err.Err = ErrSyntax.

If s is syntactically well-formed but is more than 1/2 ULP away from the largest floating-point number of the given size, ParseFloat returns f = ±Inf, err.Err = ErrRange.

ParseFloat recognizes the strings “NaN“, “+Inf“, and “-Inf” as their respective special floating-point values. It ignores case when matching.

Syntax

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

See the following example.

package main

import (
	"fmt"
	"strconv"
)

func main() {
	fValue := "19.21"
	if s, err := strconv.ParseFloat(fValue, 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseFloat(fValue, 64); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
}

Output

go run hello.go
float64, 19.209999084472656
float64, 19.21

Golang Float to string

Use the fmt.Sprintf() method to format a floating-point number as a string.

Golang Sprintf() function formats according to a format specifier and returns the resulting string.

See the following code.

package main

import (
	"fmt"
)

func main() {
	data := 11.21
	s := fmt.Sprintf("%f", data)
	fmt.Printf("%T, %v\n", s, s)
}

Output

go run hello.go
string, 11.210000

The Sprintf() function helps us to convert float to string data type.

Formatting Description Verb
1.234560e+02 Scientific notation %e
123.456000 Decimal point, no exponent %f
123.46 Default width, precision 2 %.2f
␣␣123.46 Width 8, precision 2 %8.2f
123.456 Exponent as needed, necessary digits only %g

Golang ParseFloat() function accepts decimal and hexadecimal floating-point numbers. If the string is well-formed and close to a valid floating-point number, then the function returns the closest floating-point number rounded using IFFF754 unbiased rounding. If string is not well-formed then function returns an error (err.Err = ErrSyntax).

So to convert or cast a string to float value, just use the strconv.ParseFloat() function.

See also

Golang int to string

Golang Serialize JSON String

Golang JSON to CSV

Golang string to int

Golang JSON to Map

Golang String to byte array

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.