Go 1.27 reference · Formatting, Strings and Text

Go strconv.Atoi, Itoa and ParseFloat Function

Convert between strings and numbers or booleans, with errors for invalid input.

Go 1.27

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

func Atoi(s string) (int, error)   func Itoa(i int) string   func ParseFloat(s string, bitSize int) (float64, error)

Parameters

ParameterDescription
Atoi / Itoa String to int and back.
ParseInt(s, base, bits) Other bases and sizes.
ParseFloat / FormatFloat Floats.
ParseBool "true", "1", "false"...

Examples

Example 1
package main

import (
	"fmt"
	"strconv"
)

func main() {
	n, err := strconv.Atoi("42")
	fmt.Println(n+1, err)
	_, err = strconv.Atoi("4x")
	fmt.Println(err)
	fmt.Println(strconv.Itoa(7) + "!")
}

Output

43 <nil>
strconv.Atoi: parsing "4x": invalid syntax
7!

Run this on your computer — see “Get Started” for setup.

Floats, other bases and booleans
package main

import (
	"fmt"
	"strconv"
)

func main() {
	f, _ := strconv.ParseFloat("19.99", 64)
	hex, _ := strconv.ParseInt("ff", 16, 64)
	ok, _ := strconv.ParseBool("true")
	fmt.Println(f*2, hex, ok, strconv.FormatFloat(3.14159, 'f', 2, 64))
}

Output

39.98 255 true 3.14

Run this on your computer — see “Get Started” for setup.

Was this page helpful?