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
Syntax
func Atoi(s string) (int, error) func Itoa(i int) string func ParseFloat(s string, bitSize int) (float64, error)
Parameters
| Parameter | Description |
|---|---|
Atoi / Itoa |
String to int and back. |
ParseInt(s, base, bits) |
Other bases and sizes. |
ParseFloat / FormatFloat |
Floats. |
ParseBool |
"true", "1", "false"... |
Examples
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.
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.
Related
fmt.Printf· Print formatted output using verbs like %v, %+v, %d, %s, %q, %.2f and %T, with width and alignment options.- Go Strings · tutorial