Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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
<div class="execute"></div><div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment">// Golang program to convert Int data type to Float</span>
<span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token comment">// fmt package provides the function to print anything</span>
<span class="token keyword">import</span> <span class="token punctuation">(</span>
<span class="token string">"fmt"</span>
<span class="token string">"reflect"</span>
<span class="token punctuation">)</span>
<span class="token comment">// start the main function this is the main starting point of the program</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// declare the variable and assign a value to it</span>
<span class="token keyword">var</span> number float64 <span class="token operator">=</span> <span class="token number">273.54</span>
<span class="token keyword">var</span> typeof <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">reflect<span class="token punctuation">.</span></span>TypeOf</span><span class="token punctuation">(</span>number<span class="token punctuation">)</span>
<span class="token comment">// use the int() function to convert float to int and store the output in a variable called result.</span>
<span class="token keyword">var</span> result <span class="token keyword">int</span> <span class="token operator">=</span> <span class="token keyword">int</span><span class="token punctuation">(</span>number<span class="token punctuation">)</span>
<span class="token keyword">var</span> typeof2 <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">reflect<span class="token punctuation">.</span></span>TypeOf</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span>
<span class="token comment">// printing the float type value</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"The float value ="</span><span class="token punctuation">,</span> number<span class="token punctuation">,</span><span class="token string">"\nType of variable="</span><span class="token punctuation">,</span>typeof<span class="token punctuation">)</span>
<span class="token comment">// print the int value obtained after the conversion</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Printf</span><span class="token punctuation">(</span><span class="token string">"The integer value after conversion = %d\n"</span><span class="token punctuation">,</span> result<span class="token punctuation">)</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Variable type ="</span><span class="token punctuation">,</span>typeof2<span class="token punctuation">)</span>
<span class="token comment">// reasigning the number value</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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
<div class="execute"></div><div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment">// Golang program to convert Int data type to Float</span>
<span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token comment">// fmt package provides the function to print anything</span>
<span class="token keyword">import</span> <span class="token punctuation">(</span>
<span class="token string">"fmt"</span>
<span class="token string">"reflect"</span>
<span class="token punctuation">)</span>
func <span class="token function">floatToint</span><span class="token punctuation">(</span>value float64<span class="token punctuation">)</span> <span class="token keyword">int</span> <span class="token punctuation">{</span>
<span class="token comment">// conversion code</span>
<span class="token keyword">return</span> <span class="token keyword">int</span><span class="token punctuation">(</span>value <span class="token operator">*</span> <span class="token number">1</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token comment">// start the main function this is the main starting point of the program</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// declare the variable and assign a value to it</span>
<span class="token keyword">var</span> number float64 <span class="token operator">=</span> <span class="token number">273.54</span>
<span class="token keyword">var</span> typeof <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">reflect<span class="token punctuation">.</span></span>TypeOf</span><span class="token punctuation">(</span>number<span class="token punctuation">)</span>
<span class="token comment">// call the floatToint function to convert float value to int and store the output in a // variable called result.</span>
<span class="token keyword">var</span> result <span class="token keyword">int</span> <span class="token operator">=</span> <span class="token function">floatToint</span><span class="token punctuation">(</span>number<span class="token punctuation">)</span>
<span class="token comment">// printing the float type value</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Printf</span><span class="token punctuation">(</span><span class="token string">"The float value = %.2f\n"</span><span class="token punctuation">,</span> number<span class="token punctuation">)</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Variable Type ="</span><span class="token punctuation">,</span>typeof<span class="token punctuation">)</span>
<span class="token comment">// print the int value obtained after the conversion</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Printf</span><span class="token punctuation">(</span><span class="token string">"The integer value after conversion = %d\nVaraibel Type = %T\n"</span><span class="token punctuation">,</span> result<span class="token punctuation">,</span> result<span class="token punctuation">)</span>
<span class="token comment">// printing a new line</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">// reassigining the number variable</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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.
