Golang Program to convert double type variables to int


In this tutorial we will learn how to convert double type variables to integer variables using Golang programming language.

Double stands for double precision. In programming languages, a double data-type is used to handle decimal numbers more precisely i.e. using double data type we can store more number of digits after a decimal point with accuracy

EXAMPLE 1: GO LANGUAGE CODE TO CONVERT DOUBLE TYPE VARIABLES TO INT:

Syntax

fmt.Println(int(b))
Int(x)                    

To convert a float type input value to an integer value, we can simply wrap int() around the float type variable.

Although Go can convert double to integers, the application will lose the float's precision. It functions similarly when you use it to convert between other integer types when you wrap double in int() or one of its architecture-independent data types.

Example

// Golang program to convert Int data type to Float package main // fmt package provides the function to print anything import ( "fmt" "reflect" ) // start the main function this is the main starting point of the program func main() { // declare the variable and assign a value to it var number float64 = 273.54 var typeof = reflect.TypeOf(number) // use the int() function to convert float to int and store the output in a variable called result. var result int = int(number) var typeof2 = reflect.TypeOf(result) // printing the float type value fmt.Println("The float value =", number,"\nType of variable=",typeof) // print the int value obtained after the conversion fmt.Printf("The integer value after conversion = %d\n", result) fmt.Println("Variable type =",typeof2) // reasigning the number value }

Output

The float value = 273.54
Type of variable= float64
The integer value after conversion = 273
Variable type = int                    

Go language leads the above conversion just by removing the decimal point. It omits all the digits after the decimal point. Note that it does not rounds the number to next or previous digits. If you wish to round the result to next digits you should use other mathematical functions like round().

Description of the Code

  • In the above program, we first declare the package main

  • We imported the fmt package that includes the files of package fmt and allows us to print anything on the screen.

  • Next, we start the function main() and this function is the entry point of the executable program. It does not take any argument nor return anything.

  • Now declare and initialize a double type variable number and assign value to it.

  • Next, declare an integer variable “result". Use the int() function around the float type input to convert it to the integer value.

  • Store the result in a separate variable called result.

  • Next, we print the converted int value, using the Printf() function.

EXAMPLE 2: CONVERT DOUBLE TO INT USING A CUSTOM FUNCTION

Golang's datatypes are extremely rigorous, therefore if you declare a variable's type as float64, you can only enter float numbers as values. You will simply see an error if you attempt to write an integer number into a specified variable of type float.

So you need to convert the double type of variable to integer. See the below example to see how you can perform this task manually.

Example

// Golang program to convert Int data type to Float package main // fmt package provides the function to print anything import ( "fmt" "reflect" ) func floatToint(value float64) int { // conversion code return int(value * 1) } // start the main function this is the main starting point of the program func main() { // declare the variable and assign a value to it var number float64 = 273.54 var typeof = reflect.TypeOf(number) // call the floatToint function to convert float value to int and store the output in a // variable called result. var result int = floatToint(number) // printing the float type value fmt.Printf("The float value = %.2f\n", number) fmt.Println("Variable Type =",typeof) // print the int value obtained after the conversion fmt.Printf("The integer value after conversion = %d\nVaraibel Type = %T\n", result, result) // printing a new line fmt.Println() // reassigining the number variable }

Output

The float value = 273.54
Variable Type = float64
The integer value after conversion = 273
Varaibel Type = int

Description of the Code

  • In the above program, we first declare the package main

  • We imported the fmt package that includes the files of package fmt and allows us to print anything on the screen.

  • Define a function named floatToint() which contains the lines of code to carry on the required conversion.

  • Next, we start the function main() and this function is the entry point of the executable program. It does not take any argument nor return anything.

  • Now declare and initialize a floating-point value to a variable, which we will later convert to an integer type variable.

  • Call floatToint() function and pass the above initialized float value as an argument to it.

  • Store the output obtained in a separate variable called result.

  • Next, we print the converted int value along with the data type, using the Printf() function.

Conclusion

We have successfully compiled and executed the Golang program code to convert double type variables to integer type variables in the above two examples.

Updated on: 22-Nov-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements