# Golang : Convert(cast) int to float example

Tags : golang convert type-cast int float reflect-typeof

**Problem :**

You have a variable of type int and you want to convert/type cast it to float64 type. Also, you want to use `reflect`

to confirm the variable type after conversion.

**Solution :**

Type cast the integer variable with float32(single precision floating point) or float64 and use `reflect.TypeOf()`

function to confirm.

For example :

```
package main
import (
"fmt"
"reflect"
)
func main() {
var intValue int = 1088
var floatValue float64 = float64(intValue)
fmt.Println(intValue)
fmt.Printf("The type of intValue is %v \n", reflect.TypeOf(intValue))
fmt.Println(floatValue)
fmt.Printf("The type of floatValue is %v \n", reflect.TypeOf(floatValue))
}
```

Output :

1088

The type of intValue is int

1088

The type of floatValue is float64

References :

https://www.socketloop.com/tutorials/golang-convert-cast-float-to-int

From https://golang.org/ref/spec#Types :

```
uint8 the set of all unsigned 8-bit integers (0 to 255)
uint16 the set of all unsigned 16-bit integers (0 to 65535)
uint32 the set of all unsigned 32-bit integers (0 to 4294967295)
uint64 the set of all unsigned 64-bit integers (0 to 18446744073709551615)
int8 the set of all signed 8-bit integers (-128 to 127)
int16 the set of all signed 16-bit integers (-32768 to 32767)
int32 the set of all signed 32-bit integers (-2147483648 to 2147483647)
int64 the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)
float32 the set of all IEEE-754 32-bit floating-point numbers
float64 the set of all IEEE-754 64-bit floating-point numbers
complex64 the set of all complex numbers with float32 real and imaginary parts
complex128 the set of all complex numbers with float64 real and imaginary parts
byte alias for uint8
rune alias for int32
```

#### See also : Golang : Convert(cast) float to int

Tags : golang convert type-cast int float reflect-typeof

##### By Adam Ng

IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.

#### Advertisement

#### Tutorials

+8.2k CodeIgniter/PHP : Create directory if does not exist example

+3k Golang : Convert(cast) io.Reader type to string

+8.3k Golang : XML to JSON example

+5.5k Golang : Calculate percentage change of two values

+4.7k Golang : Verify token from Google Authenticator App

+2.2k Golang : Add build version and other information in executables

+17.6k Golang : How to stream file to client(browser) or write to http.ResponseWriter?

+2.5k Golang : Check if integer is power of four example

+1.7k Javascript : How to get width and height of a div?

+2.7k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference

+6.3k Golang : List objects in AWS S3 bucket

+4.1k Golang : Linear algebra and matrix calculation example