Finding the Integer Value of Specified Number in Golang


In Go language, integers are numeric values that represent whole numbers without a fractional component. Integers can be signed or unsigned, and can be of various sizes depending on the number of bits used to represent them.

Sometimes it is necessary to obtain the integer value of a specified number in order to perform various calculations. In this article, we will discuss how to find the integer value of a given number in Golang.

Using Type Casting to Convert a Float to an Integer

One way to obtain the integer value of a given number in Golang is to use type casting. Type casting is the process of converting a value of one data type to another data type. In this case, we can convert a float value to an integer value by using type casting.

Here is an example of how to use type casting to find the integer value of a given number −

Example

package main

import "fmt"

func main() {
   var num float64 = 3.14
   integer := int(num)
   fmt.Println("The integer value of", num, "is", integer)
}

Output

The integer value of 3.14 is 3

In this example, we have declared a variable num of type float64 and assigned it the value 3.14. We then use the int() function to convert the float value to an integer value, and assign the result to a new variable called integer. Finally, we print out the value of integer to the console using the fmt.Println() function.

Using the Math Package to Round a Float to the Nearest Integer

Another way to find the integer value of a given number in Golang is to use the math package. The math package provides several functions for mathematical operations, including rounding a float to the nearest integer.

Here is an example of how to use the math package to find the integer value of a given number −

Example

package main

import (
   "fmt"
   "math"
)

func main() {
   var num float64 = 3.14
   integer := int(math.Round(num))
   fmt.Println("The integer value of", num, "is", integer)
}

Output

The integer value of 3.14 is 3

In this example, we have imported the math package and declared a variable num of type float64 with the value 3.14. We then use the math.Round() function to round the float value to the nearest integer, and use type casting to convert it to an integer value. Finally, we print out the value of integer to the console using the fmt.Println() function.

Using the strconv Package to Convert a String to an Integer

If you have a string that represents a numeric value, you can use the strconv package to convert it to an integer value. The strconv package provides several functions for converting strings to different data types.

Here is an example of how to use the strconv package to find the integer value of a given string −

Example

package main

import (
   "fmt"
   "strconv"
)

func main() {
   str := "42"
   integer, err := strconv.Atoi(str)
   if err != nil {
      fmt.Println(err)
   } else {
      fmt.Println("The integer value of", str, "is", integer)
   }
}

Output

The integer value of 42 is 42

In this example, we have declared a variable str with the value "42". We then use the strconv.Atoi() function to convert the string value to an integer value, and assign the result to a new variable called integer. If there is an error during the conversion, we print the error message to the console using the fmt.Println() function. Otherwise, we print out the value of `integer

Using the Floor Function

The math.Floor() function in Golang can be used to extract the integer part of a floating-point number. This function takes a floating-point number as an argument and returns the largest integer value less than or equal to the given number. Here's an example −

Example

package main

import (
   "fmt"
   "math"
)

func main() {
   var x float64 = 4.5
   fmt.Println(math.Floor(x))
}

Output

4

Using the ParseInt Function

The strconv package in Golang provides a function called ParseInt that can be used to extract the integer value of a hexadecimal number. Here's an example −

Example

package main

import (
   "fmt"
   "strconv"
)

func main() {
   var hexStr string = "1a"
   var base int = 16
   var bitSize int = 64

   hexVal, err := strconv.ParseInt(hexStr, base, bitSize)
   if err == nil {
      fmt.Println(hexVal)
   }
}

Output

26

In the above example, we have used the strconv.ParseInt() function to extract the integer value of the hexadecimal string 1a. The base parameter is set to 16, which specifies that the string is a hexadecimal number. The bitSize parameter is set to 64, which specifies that the returned integer value should be a 64-bit integer. The function returns 26, which is the decimal equivalent of the hexadecimal number 1a.

Conclusion

In this article, we have seen different ways to extract the integer part of a number in Golang.

Updated on: 12-Apr-2023

84 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements