Software Engineering Wiki

Digits

Converts an integer to an array of digits.

  • Use strconv.Itoa() to convert the given number to a string, make() and len() to create an appropriate slice.
  • Use range in combination with strings.Split() to iterate over the digits, converting them to int using strconv.Atoi().
import (
	"strconv"
	"strings"
)

func Digits(n int) []int {
	s := strconv.Itoa(n)
	d := make([]int, len(s))
	for i, l := range strings.Split(s, "") {
		d[i], _ = strconv.Atoi(l)
	}
	return d
}
Digits(123) // [1 2 3]

Last updated 15 September 2026 · Edit this page