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.
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:
- The string to parse.
- The base of the number being parsed. (e.g. base 2/binary = “10101100”, base 8/octal = “557”, base 16/hexadecimal = “38b4ef27”)
- 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
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.
For unsigned ints, everything above still applies, but use
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.
There is an equivalent method for parsing floats:
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
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.
ParseInt, there is no way to change the base, meaning there are only two parameters:
- The string to parse.
- The desired float size, either 32 or 64.
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)
Go forth and convert strings to other things.