how to convert float64 to float32 in golang?

Hi Friends 👋,

Welcome To aGuideHub! ❤️

To convert float64 to float32 in golang, use the float32() method it will return float64 to float32. You have to just call it float32(v).


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 float64 to float32 in golang, as mentioned above I’m going to use the float32() method which is built-in provided by golang.

Let’s start our Golang convert float64 to float32 example

main.go

package main

import (
	"fmt"
	"reflect"
)

func main() {
	var f64 = 1097.655698798798
	fmt.Println(f64)
	fmt.Println(reflect.TypeOf(f64))

	var f32 = float32(f64)
	fmt.Println(f32)
	fmt.Println(reflect.TypeOf(f32))
}

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

Output

1097.655698798798
float64
1097.6556
float32

I hope it helps you, All the best 👍.

Try it Yourself

Premium Content

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

Books

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.

I'm working on some more Cheat Sheets book on Jquery, TypeScript, React js and for other languages. I will send it once it's completed.

Stay tuned working on React Js Cheat Sheets Book

Related Posts