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 👍.

Follow me on Twitter