How to Parse Numbers from Strings

This post will cover how to convert a string into a number, this is split into three broad groups, ints, unsigned ints, and floats. I’ll quickly run through each. All the parsing methods sit within the strconv package, extended doco here.

Integers

If you’re working with the standard int type, then the Atoi method is for you.

i, err := strconv.Atoi("25")

As you would expect, i will be of type int, and have the value 25, and err will be nil. If there are errors in the conversion then err will contain the resulting error for you to deal with as you see fit. i is not guaranteed to be 0.

If your string is not base 10, or if you’re not dealing with the int type, but instead using a specifically sized integer, e.g. int8, then you need to use ParseInt(...) which takes three arguments:

  1. The string to parse.
  2. The base of the number being parsed. (e.g. base 2/binary = “10101100”, base 8/octal = “557”, base 16/hexadecimal = “38b4ef27”)
  3. The number of bits in the type you’ll put the value in. (e.g. int16 will need a value of 16.) If this is set to 0, the number of bits will match the number of bits in an int (system dependent).

Some examples of changing the base:

decimal, err := strconv.ParseInt("100", 10, 64)
binary, err := strconv.ParseInt("100", 2, 64)
octal, err := strconv.ParseInt("52", 8, 64)
hexadecimal, err := strconv.ParseInt("FF", 16, 64)

And changing the output size:

i, err := strconv.ParseInt("100", 10, 0)
i8, err := strconv.ParseInt("100", 10, 8)
i16, err := strconv.ParseInt("30000", 10, 16)
i32, err := strconv.ParseInt("2000000000", 10, 32)
i64, err := strconv.ParseInt("9000000000000000000", 10, 64)

Note: All the above objects will actually be of type int64, the package guarantees that (if there are no errors) the result can be cast to the applicable int. From the example above, the following can be done without losing any information:

var realI8 int8
realI8 = int8(i8)

If the number being parsed won’t fit, or contains invalid characters, err will be non-nil. In this case, the returned object can’t be trusted and you should handle the error appropriately.

Unsigned Integers

For unsigned ints, everything above still applies, but use strconv.ParseUint.

The obvious difference is that 8 bit numbers will need to be between 0 and 255 instead of between -128 and 127. Similar restrictions apply for other int sizes.

Floats

There is an equivalent method for parsing floats: strconv.ParseFloat(...)

Like ParseInt, you are able to specify the number of bits in the output, the options are limited to 32 and 64 though and correspond to a float32 and float64 respectively. Also like ParseInt, regardless of what bit value is passed in, the output is fixed at the largest type, float64. It is guaranteed, however, that the result will be cast-able to the desired type.

Unlike ParseInt, there is no way to change the base, meaning there are only two parameters:

  1. The string to parse.
  2. The desired float size, either 32 or 64.

For example:

f32, err := strconv.ParseFloat("1.234", 32)
f64, err := strconv.ParseFloat("1.234", 64)

All the usual float formats are accepted:

f1, err := strconv.ParseFloat("1.234", 64)
f2, err = strconv.ParseFloat("2.46e+12", 64)
f3, err = strconv.ParseFloat("7.33E-09", 64)
Conclusion

Go forth and convert strings to other things.

Leave a Reply

Your email address will not be published. Required fields are marked *