 is coming soon... # Converting Strings to Numbers in Go

May 9, 2020

Go is a strongly-typed programming language. As we write programs in Go, we’ll often have to convert numbers (i.e integers and floats) to strings and vice versa. In this post, I’ll show you how to do just that.

The Go standard library gives us all the tools we need to convert ints to strings, strings to ints, and much more. The package we’ll be working with is called strconv . Let’s see some examples.

## Converting an Int to String in Go

Before we dive into string to integer conversions, let’s quickly see how we can convert an integer to a string in Go. To convert an integer value in Go to a string, we’ll make use of the Itoa function provided by the strconv package.

``````package main

import "fmt"

func main() {
num := 100
message := strconv.Itoa(num)

fmt.Println(message) // "100"
}
``````

Note that this only works if the type of num is `int`. If you tried to pass in an `int8` or `int64` for example, the program would error out.

## Converting a String to an Int in Go

To convert a string value to an int in Go, you would use the inverse Atoi function.

``````package main

import (
"fmt"
"strconv"
)

func main() {
message := "100"
num, err := strconv.Atoi(message)

if err != nil {
fmt.Println(err)
}

fmt.Println(num) // 100
}
``````

There are some caveats to remember with the `Atoi` function as well. For one, the number must be within the allowed range for a type int in Go, which is depending on the platform that is running the code. For example, a 64-bit system can store an int value up to 9223372036854775807. The second is that it must be a valid number. So, if the string has non-numeric characters, the `Atoi` function will throw an error.

## A More Powerful Way to Convert Strings to Integers in Go

The `Atoi` function often gets the job done just fine, but sometimes you need a little more. This is where the `ParseInt` and `ParseUint` functions come in and provide greater functionality and flexibility.

These functions are a little more complex to use as they require a couple additional parameters. Let’s see an example.

``````package main

import (
"fmt"
"strconv"
)

func main() {
message := "100"
num, err := strconv.ParseInt(message, 0, 64)
unum, err := strconv.ParseUint(message, 0, 64)

if err != nil {
fmt.Println(err)
}

fmt.Println(num, unum) // 100 100
}
``````

Both the `ParseInt` and the `ParseUint` functions take three parameters and work pretty much the same, the only difference is that the `ParseInt` functions returns an integer of type `int64`, while the `ParseUint` functions returns an integer of the type `uint64`.

The three parameters for both are functions are as follows:

• The string to try and convert to an integer
• The base the string value is in
• The bit size that the resulting integer must fit into

The first parameter is pretty straightforward. Just supply the string you wish to convert. The second argument allows you to denote the base the string is in. If you’re working with base 10, then you can pass in the argument of either “0” or “10”. Finally, for the bit size, the value should be either 0, 8, 16, 32, and 64, which will correspond to the Go int types of int, int8, etc.

## Converting Strings to Floats in Go

Go also allows you to convert string values to floats. To do this, you’ll use the `ParseFloat` function in the `strconv` package. The `ParseFloat` function takes two arguments, the string to convert and floating point precision denoted by a `bitSize`. Let’s see an example.

``````package main

import (
"fmt"
"strconv"
)

func main() {
message := "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679"
num1, err := strconv.ParseFloat(message, 32)
num2, err := strconv.ParseFloat(message, 64)

if err != nil {
fmt.Println(err)
}

fmt.Println(num1, num2) // 3.1415927410125732 3.141592653589793
}
``````

In our example, we are converting the string value containing the first 100 digits of Pi using both 32 and 64-bit precision. As you can see in the results, with 32-bit precision only the first 6 digits are the same, before diverging.

The Go standard library is very extensive and the `strconv` package features some very powerful functions for converting between various types. In this post, we saw how to convert integer values to strings and the various ways we can convert strings into various numeric types in Go, such as ints and floats.