strconv

strconv Atoi, ParseInt, and FormatInt, including base, bitSize, syntax errors, and out-of-range values.

模块页正文目前为英文。

The strconv package converts between basic types and their string representations. It is the standard-library way to parse integers and floats from text. fmt.Sscan can also parse numbers, but strconv gives you explicit bases, bit sizes, and error values.

This page focuses on Atoi, Itoa, ParseInt, ParseFloat, and FormatInt. It does not cover Append* helpers or Quote with every escape.

Examples were checked against Go 1.24.0. See the official package documentation for the complete API.

Go reference overview →

API index

APISummary
strconv.Atoi, strconv.ItoaParse a base-10 integer into an int, or format an int as a base-10 string.
strconv.ParseInt, strconv.FormatInt, strconv.ParseFloatParse or format integers with an explicit base and bit size, or parse a floating-point number. bitSize limits the range; it does not change the fact that ParseInt itself returns int64.

Atoi and Itoa

Atoi is equivalent to ParseInt(s, 10, 0) converted to int. Itoa is equivalent to FormatInt(int64(i), 10). Both use the platform int size, which is 64 bits on the machine used for these examples.

strconv.Atoi, strconv.Itoa

func Atoi(s string) (int, error)
func Itoa(i int) string

Parse a base-10 integer into an int, or format an int as a base-10 string.

ParameterDescription
sA decimal integer, optionally with a leading sign. Leading zeros are allowed; a trailing unit such as px is not.
iThe integer to format. Itoa does not return an error.

Returns: Atoi returns the parsed int and a nil error on success. Itoa returns the decimal string.

Errors

  • *strconv.NumError — On failure, Atoi returns 0 and a *NumError. NumError.Err is strconv.ErrSyntax when the text is not a decimal integer, or strconv.ErrRange when the value does not fit in an int.
  • Atoi("80px") is a syntax error, not a successful parse of 80. There is no equivalent of parseInt stopping at the first non-digit.
go
package main

import (
	"fmt"
	"strconv"
)

func main() {
	n, err := strconv.Atoi("80")
	fmt.Println(n, err)
	_, err = strconv.Atoi("80px")
	fmt.Println(err)
	fmt.Println(strconv.Itoa(-80))
}

Expected output

80 <nil>
strconv.Atoi: parsing "80px": invalid syntax
-80

Official documentation →

ParseInt base and bitSize

ParseInt(s, base, bitSize) parses a signed integer. base is 2 through 36, or 0 to infer the base from a prefix: 0b, 0o or a leading 0 for octal, 0x for hexadecimal, otherwise decimal. bitSize is 0, 8, 16, 32, or 64 and names the integer type the value must fit; 0 means int.

When the number is valid syntax but does not fit, the error is ErrRange and the returned value is the maximum or minimum of that type. FormatInt writes an int64 in the requested base, without a 0x prefix.

strconv.ParseInt, strconv.FormatInt, strconv.ParseFloat

func ParseInt(s string, base int, bitSize int) (i int64, err error)
func FormatInt(i int64, base int) string
func ParseFloat(s string, bitSize int) (float64, error)

Parse or format integers with an explicit base and bit size, or parse a floating-point number. bitSize limits the range; it does not change the fact that ParseInt itself returns int64.

ParameterDescription
sThe text to parse. For base 0, prefixes such as 0x select the base.
base2–36, or 0 to detect a prefix. Atoi is base 10 and does not accept 0xff.
bitSizeFor ParseInt: 0, 8, 16, 32, or 64. For ParseFloat: 32 or 64. ParseFloat still returns float64; bitSize 32 rounds to float32 range and precision.

Returns: ParseInt always returns int64, even when bitSize is 32 or 8. FormatInt returns the digits in the requested base, using lowercase letters for bases above 10.

Errors

  • *strconv.NumError — ErrSyntax when the text cannot be parsed, ErrRange when the value does not fit in the selected bit size. On ErrRange, the int64 result is the max or min of that size, not 0.
  • ParseInt("ff", 16, 64) and ParseInt("0xff", 0, 64) both yield 255.
  • ParseInt("300", 10, 8) does not fit in an int8, so the result is 127 and the error is "value out of range".
  • ParseInt("2147483648", 10, 32) overflows int32. The result is 2147483647 with ErrRange.
go
package main

import (
	"fmt"
	"strconv"
)

func main() {
	v, err := strconv.ParseInt("ff", 16, 64)
	fmt.Println(v, err)
	v, err = strconv.ParseInt("0xff", 0, 64)
	fmt.Println(v, err)
	v, err = strconv.ParseInt("300", 10, 8)
	fmt.Println(v, err)
	v, err = strconv.ParseInt("2147483648", 10, 32)
	fmt.Println(v, err)
	f, err := strconv.ParseFloat("1.25", 64)
	fmt.Println(f, err)
	fmt.Println(strconv.FormatInt(255, 16))
}

Expected output

255 <nil>
255 <nil>
127 strconv.ParseInt: parsing "300": value out of range
2147483647 strconv.ParseInt: parsing "2147483648": value out of range
1.25 <nil>
ff

Official documentation →