strconv.FormatInt()
functionThe strconv
package’s FormatInt()
function is used to obtain the string representation of a given integer in a chosen base, which can range from 2 to 36 (2 <= base <= 36). For digit values greater >= 10, the output uses lower-case letters a
to z
.
func FormatInt(i int64, base int) string
i
: The integer value to be converted in the string format.base
: The given base value.The function FormatInt()
returns the string representation of the given integer in the given base.
The following code shows how to implement the strconv.FormatInt()
function in Go.
// Using strconv.FormatFloat() Function package main import ( "fmt" "strconv" ) func main() { // declaring variable var num int64 // Assign value num = 2634 // returns a string type fmt.Println(strconv.FormatInt(num, 10)) fmt.Println() // reassign value num = 186 // returns a string type fmt.Println(strconv.FormatInt(num, 2)) fmt.Println() // reassign value num = 86 // returns a string type fmt.Println(strconv.FormatInt(num, 16)) fmt.Println() }
main
package.main()
function, variable num of int64
type, and assign a value to it. Next, we pass the variable to the FormatInt()
function, which converts the integer value to a string given the base
parameter.RELATED TAGS
CONTRIBUTOR
View all Courses