Number Parsing in Golang


Number parsing in Go is about converting the numbers that are present in string form to number form. By number form, we mean that these numbers can either be converted into integers, floats, etc.

The most widely used package for number parsing is the "strconv" package that Go library provides us with. There are many cases that are present in the number parsing in Go, we will talk about all of these one by one in this article.

The most basic approach is when we have a base 10 number that is actually present in a string form and we want to convert it into an integer. In order to do that we can make use of the most widely used Atoi() function.

The syntax for the Atoi() function looks something like this.

result, error := strconv.Atoi(String)

Now, let's consider that we have a string which contains the value "191" and we want to convert it into an integer.

Example

Consider the code shown below.

package main
import (
   "fmt"
   "strconv"
)
func main() {
   var value string = "123"
   res, err := strconv.Atoi(value)
   if err != nil {
      fmt.Println("Error encountered:", err)
      return
   }
   fmt.Println("The number now is:", res)
   fmt.Printf("The type of the number is: %T", res)
}

Output

If we run the code with the command go run main.go, then we will see the following output in the terminal.

The number now is: 123
The type of the number is: int

Now, the second most common number parsing is when we want to convert a number that is present in a string form into a 64-bit number. For that we either use ParseInt() or ParseFloat() functions that are present inside the strconv itself.

Let's consider examples of both of these functions, as one of these functions is used when we want to parse a floating point number and the other when we want to pass a 64-bit integer value.

Syntax of ParseInt()

number, error := strconv.ParseInt(string, base, bitSize)

Syntax of ParseFloat()

number, error := strconv.ParseFloat(string, bitSize)

Example 2

In this example, we will use both ParseInt() and ParseFloat() functions.

package main
import (
   "fmt"
   "strconv"
)
func main() {
   var value string = "123"
   var value1 string = "1.23"
   res, err := strconv.ParseInt(value, 0, 64)
   if err != nil {
      fmt.Println("Error encountered:", err)
      return
   }
   fmt.Println("The number now is:", res)
   fmt.Printf("The type of the number is: %T \n", res)
   res1, err1 := strconv.ParseFloat(value1, 64)
   if err1 != nil {
      fmt.Println("Error encountered:", err1)
      return
   }
   fmt.Println("The number now is:", res1)
   fmt.Printf("The type of the number is: %T", res1)
}

Output

If we run the code with the command go run main.go, then we will see the following output in the terminal.

The number now is: 123
The type of the number is: int64
The number now is: 1.23
The type of the number is: float64

An important point to note about the ParseInt() function is that it can also recognize hex-formatted numbers.

Let's suppose that we have a hexadecimal number "0x1F3" which is present in a string form and we want to convert it into an integer. For this purpose, we can use only ParseInt() function.

Example 3

Consider the code shown below.

package main
import (
   "fmt"
   "strconv"
)
func main() {
   var value string = "0x1F3"
   res, err := strconv.ParseInt(value, 0, 64)
   if err != nil {
      fmt.Println("Error encountered:", err)
      return
   }
   fmt.Println("The number now is:", res)
   fmt.Printf("The type of the number is: %T \n", res)
}

Output

If we run the code with the command go run main.go, then we will see the following output in the terminal.

The number now is: 499
The type of the number is: int64

Updated on: 21-Feb-2022

562 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements