Skip to content
Tutorials Center
My learning

Topics

Go Reference Β· strings and strconv

Go strconv.ParseInt(), ParseFloat() and ParseBool()

Reference for Go strconv.ParseInt, ParseUint, ParseFloat and ParseBool: bases, bit sizes, accepted syntax, range errors and the exact values each one returns.

When Atoi is too limited, the Parse functions in strconv give you control. ParseInt and ParseUint read integers in any base from 2 to 36 and check that the result fits a chosen bit size. ParseFloat reads floating-point numbers, and ParseBool reads the handful of spellings Go accepts for true and false.

All of them return the parsed value and an error, and all report failures as a *strconv.NumError that wraps either strconv.ErrSyntax or strconv.ErrRange.

Go
v, err := strconv.ParseInt("ff", 16, 64) // 255

Try it

Go Bases, bit sizes and prefixes

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go Floats and booleans

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Go
func ParseInt(s string, base int, bitSize int) (int64, error)
func ParseUint(s string, base int, bitSize int) (uint64, error)
func ParseFloat(s string, bitSize int) (float64, error)
func ParseBool(str string) (bool, error)
Name Description
s The text to parse. No surrounding whitespace is allowed.
base 2 to 36, or 0 to infer from a prefix: 0x, 0b, 0o, or a leading 0 for octal; otherwise 10.
bitSize Target size: 0 (means int/uint), 8, 16, 32 or 64. For ParseFloat: 32 or 64.

Returns: ParseInt an int64 (convert it to a smaller type after checking), ParseUint a uint64 (no sign allowed), ParseFloat a float64, ParseBool a bool.

Errors and edge cases:

  • Empty or malformed text gives ErrSyntax and the value 0.
  • A value that does not fit bitSize gives ErrRange with the nearest limit (for example 127 for int8).
  • An invalid base or bit size gives a NumError whose cause is an "invalid base" or "invalid bit size" error, not ErrSyntax.
  • ParseFloat returns Β±Inf with ErrRange on overflow, and accepts inf, infinity and nan in any letter case.
  • ParseBool accepts exactly 1, t, T, TRUE, true, True and 0, f, F, FALSE, false, False.

Usage notes

  • Choose the bit size to validate input. ParseInt(s, 10, 32) rejects values that would overflow an int32, so a later int32(v) conversion is safe.
  • bitSize for floats is about precision. With 32, the result is a float64 that holds a value already rounded to float32.
  • Underscores are accepted only when base is 0, and only between digits or after a prefix.
  • ParseBool is strict. "yes", "on" and " true" fail; normalize or write your own mapping for user-facing input.
  • Locale ignorance. "1,5" is a syntax error; replace the comma first if you accept European decimals.
  • JSON numbers are better decoded with encoding/json than hand-parsed.