How to Parse and Format Numbers as Strings in Go

Convert Go numbers to strings using strconv.FormatInt and parse them back using strconv.ParseInt.

Use strconv.FormatInt to convert integers to strings and strconv.ParseInt to parse strings back into integers.

package main

import (
	"fmt"
	"strconv"
)

func main() {
	// Int to String
	num := int64(12345)
	str := strconv.FormatInt(num, 10)
	fmt.Println(str) // Output: 12345

	// String to Int
	parsed, err := strconv.ParseInt(str, 10, 64)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	fmt.Println(parsed) // Output: 12345
}

For floating-point numbers, use strconv.FormatFloat and strconv.ParseFloat instead.