Open In App

How to convert Int data type to Float in Golang?

Last Updated : 10 May, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

In Golang, the data types are bound to variables rather than the values, which means that, if you declare a variable as int, then you can store only integer type value in it, you cant assign character or string in it unless you convert the data type to required data type.

To convert an integer data type to float you can wrap the integer with float64() or float32.

Example:




// Golang program to convert Int data type to Float
package main
  
import (
    "fmt"
    "reflect"
)
  
func main() {
  
    // var declared x as int
    var x int64 = 5
  
    // y is a float64 type variable
    var y float64 = float64(x)
  
    // printing the values of x and y
    fmt.Printf("x = %d \n", x)
    fmt.Printf("y = %f \n", y)
  
    // to print a float upto a
    // specific number of decimal point
    fmt.Printf("\ny upto 3 decimal = %.3f", y)
  
    // getting the converted type
    fmt.Println("\n", reflect.TypeOf(y))
  
}


Output

x = 5 
y = 5.000000 

y upto 3 decimal = 5.000
 float64

Explanation: Firstly we declare a variable x of type int64 with a value of 5. Then we wrap x with float64(), which converts the integer 5 to float value of 5.00. The %.3f formats the float value upto 3 decimal points.


Similar Reads

How to Convert string to float type in Golang?
ParseFloat function is an in-build function in the strconv library which converts the string type into a floating-point number with the precision specified by bit size. Example: In this example the same string -2.514 is being converted into float data type and then their sum is being printed. Once it is converted to 8 bit-size and other times it is
1 min read
Converting a string variable into Boolean, Integer or Float type in Golang
Various types of string conversions are needed while performing tasks in Golang. Package strconv is imported to perform conversions to and from string. String to Boolean Conversion ParseBool is used to convert string to boolean value. It accepts 1, t, T, TRUE, true, True as true and 0, f, F, FALSE, false, False as false. Any other value returns an
3 min read
Golang | Searching an element of int type in slice of ints
In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. In the Go slice, you can search an element of int type in the given slice of ints
3 min read
How to Get Int Type Random Number in Golang?
Go language provides inbuilt support for generating random numbers of the specified type with the help of a math/rand package. This package implements pseudo-random number generators. These random numbers are generated by a source and this source produces a deterministic sequence of values whenever the program run. And if you want to random numbers
2 min read
Type Casting or Type Conversion in Golang
Prerequisite: Golang Data Types Type conversion happens when we assign the value of one data type to another. Statically typed languages like C/C++, Java, provide the support for Implicit Type Conversion but Golang is different, as it doesn't support the Automatic Type Conversion or Implicit Type Conversion even if the data types are compatible. Th
2 min read
How to Convert string to integer type in Golang?
Strings in Golang is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. In Go language, both signed and unsigned integers are available in four different sizes. In order to convert string to integer type in Golang, you can use the following methods. 1. Atoi() Function: Th
2 min read
Different Ways to Convert the Boolean Type in String in Golang
In order to convert Boolean Type to String type in Golang , you can use the strconv and fmt package function. 1. strconv.FormatBool() Method: The FormatBool is used to Boolean Type to String. It returns "true" or "false" according to the value of b. Syntax: func FormatBool(b bool) string Example : Convert an Boolean Type into String using strconv.F
2 min read
Finding Complementary Error Function of Float in Golang
Go language provides inbuilt support for basic constants and mathematical functions to perform operations on the numbers with the help of the math package. You are allowed to find the complementary error function of the specified number with the help of the Erfc() function provided by the math package. So, you need to add a math package in your pro
2 min read
reflect.Float() Function in Golang with Examples
Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.Float() Function in Golang is used to get the v's underlying value, as a float64. To access this function, one needs to imports the reflect package in the program.
1 min read
reflect.Int() Function in Golang with Examples
Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.Int() Function in Golang is used to get the v's underlying value, as an int64. To access this function, one needs to imports the reflect package in the program. Sy
1 min read
Article Tags :