Golang strconv.FormatInt() Function with Examples

Golang | strconv.FormatInt() Function: Here, we are going to learn about the FormatInt() function of the strconv package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 07, 2021

strconv.FormatInt()

The FormatInt() function is an inbuilt function of the strconv package which is used to get the string representation of the given integer in the given base, where the base can be between 2 to 36 (2 <= base <= 36). The result uses the lower-case letters 'a' to 'z' for digit values >= 10.

It accepts two parameters (i, base) and returns the string representation of the given integer in the given base.

Syntax:

func FormatInt(i int64, base int) string

Parameter(s):

  • i : An integer value that is to be converted in the string format.
  • base : Base of the given value.

Return Value:

The return type of the FormatInt() function is a string, it returns the string representation of the given integer in the given base.

Example 1:

// Golang program to demonstrate the
// example of strconv.FormatInt() Function

package main

import (
	"fmt"
	"strconv"
)

func main() {
	fmt.Println(strconv.FormatInt(108, 10))
	fmt.Println(strconv.FormatInt(108, 2))
	fmt.Println(strconv.FormatInt(108, 8))
	fmt.Println(strconv.FormatInt(108, 16))
	fmt.Println()

	fmt.Println(strconv.FormatInt(-108, 10))
	fmt.Println(strconv.FormatInt(-108, 2))
	fmt.Println(strconv.FormatInt(-108, 8))
	fmt.Println(strconv.FormatInt(-108, 16))
}

Output:

108
1101100
154
6c

-108
-1101100
-154
-6c

Example 2:

// Golang program to demonstrate the
// example of strconv.FormatInt() Function

package main

import (
	"fmt"
	"strconv"
)

func main() {
	var x int64
	var y int64
	var result string

	x = 108
	result = strconv.FormatInt(x, 10)
	fmt.Printf("x: Type %T, value %v\n", x, x)
	fmt.Printf("result: Type %T, value %q\n", result, result)
	fmt.Println()

	y = 64
	result = strconv.FormatInt(y, 10)
	fmt.Printf("y: Type %T, value %v\n", y, y)
	fmt.Printf("result: Type %T, value %q\n", result, result)
	fmt.Println()

	z := x + y
	result = strconv.FormatInt(z, 10)
	fmt.Printf("z: Type %T, value %v\n", z, z)
	fmt.Printf("result: Type %T, value %q\n", result, result)
}

Output:

x: Type int64, value 108
result: Type string, value "108"

y: Type int64, value 64
result: Type string, value "64"

z: Type int64, value 172
result: Type string, value "172"

Example 3:

// Golang program to demonstrate the
// example of strconv.FormatInt() Function

package main

import (
	"fmt"
	"strconv"
)

func Int2String(num int64) string {
	// to convert an integer number
	// to a string
	return strconv.FormatInt(num, 10)
}

func main() {
	fmt.Println(Int2String(123456))
	fmt.Println(Int2String(-1123456))
}

Output:

123456
-1123456

Example 4:

// Golang program to demonstrate the
// example of strconv.FormatInt() Function

package main

import (
	"fmt"
	"strconv"
)

func main() {
	var (
		x int64 = 108
		y int64 = 255
		z int64 = 65535
	)

	result1 := strconv.FormatInt(x, 10)
	fmt.Printf("%T, %v\n", result1, result1)

	result2 := strconv.FormatInt(x, 16)
	fmt.Printf("%T, %v\n", result2, result2)

	result3 := strconv.FormatInt(x, 2)
	fmt.Printf("%T, %v\n", result3, result3)

	result4 := strconv.FormatInt(y, 10)
	fmt.Printf("%T, %v\n", result4, result4)

	result5 := strconv.FormatInt(z, 16)
	fmt.Printf("%T, %v\n", result5, result5)
}

Output:

string, 108
string, 6c
string, 1101100
string, 255
string, ffff

Golang strconv Package »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.