how to convert string to float in golang?

Hi Friends 👋,

Welcome To aGuideHub! ❤️

To convert string to float in golang, just use ParseFloat() method and pass your string, it will convert string into float.


Follow the below tutorial if you are struggling with installing GO in windows.

https://aguidehub.com/blog/how-to-install-golang-in-windows/


Today, I will show you how do I convert string to float in golang, as above mentioned I’m going to use ParseFloat() way.

The strconv package provide ParseFloat() method.

Let’s start our Golang convert string to float example

Convert whole string into float example

main.go

package main

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

func main() {
	s := "7.86"
	f, err := strconv.ParseFloat(s, 8)
	fmt.Println(f, err, reflect.TypeOf(f))
}

In the above example, we have converted string to float and printed in golang console. let’s check the output.

Output

7.86 <nil> float64

Try it Yourself

I hope it helps you, All the best 👍.

Premium Content

You can get all the below premium content directly in your mail when you subscribe us

Books

Interview Questions

Soon You will get CSS, JavaScript, React Js, and TypeScript So Subscribe to it.

Portfolio Template

View | Get Source Code

Cheat Sheets

Cheat Sheets Books are basically Important useful notes which we use in our day-to-day life.

Related Posts