Home »
Golang »
Golang Reference
Golang strconv.FormatUint() Function with Examples
Golang | strconv.FormatUint() Function: Here, we are going to learn about the FormatUint() function of the strconv package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 07, 2021
strconv.FormatUint()
The FormatUint() function is an inbuilt function of the strconv package which is used to get the string representation of the given unsigned integer in the given base, where 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 unsigned integer in the given base.
Syntax
func FormatUint(i uint64, base int) string
Parameters
- i : An unsigned integer value that is to be converted in the string format.
- base : Base of the given value.
Return Value
The return type of FormatUint() function is a string, it returns the string representation of the given unsigned integer in the given base.
Example 1
// Golang program to demonstrate the
// example of strconv.FormatUint() Function
package main
import (
"fmt"
"strconv"
)
func main() {
fmt.Println(strconv.FormatUint(108, 10))
fmt.Println(strconv.FormatUint(108, 2))
fmt.Println(strconv.FormatUint(108, 8))
fmt.Println(strconv.FormatUint(108, 16))
}
Output:
108
1101100
154
6c
Example 2
// Golang program to demonstrate the
// example of strconv.FormatUint() Function
package main
import (
"fmt"
"strconv"
)
func main() {
var x uint64
var y uint64
var result string
x = 108
result = strconv.FormatUint(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.FormatUint(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.FormatUint(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 uint64, value 108
result: Type string, value "108"
y: Type uint64, value 64
result: Type string, value "64"
z: Type uint64, value 172
result: Type string, value "172"
Example 3:
// Golang program to demonstrate the
// example of strconv.FormatUint() Function
package main
import (
"fmt"
"strconv"
)
func UInt2String(num uint64) string {
// to convert an integer number
// to a string
return strconv.FormatUint(num, 10)
}
func main() {
fmt.Println(UInt2String(123456))
fmt.Println(UInt2String(1234567897642))
}
Output:
123456
1234567897642
Example 4:
// Golang program to demonstrate the
// example of strconv.FormatUint() Function
package main
import (
"fmt"
"strconv"
)
func main() {
var (
x uint64 = 255
y uint64 = 65535
z uint64 = 1234567890123
)
result1 := strconv.FormatUint(x, 10)
fmt.Printf("%T, %v\n", result1, result1)
result2 := strconv.FormatUint(x, 16)
fmt.Printf("%T, %v\n", result2, result2)
result3 := strconv.FormatUint(x, 2)
fmt.Printf("%T, %v\n", result3, result3)
result4 := strconv.FormatUint(y, 10)
fmt.Printf("%T, %v\n", result4, result4)
result5 := strconv.FormatUint(z, 16)
fmt.Printf("%T, %v\n", result5, result5)
}
Output:
string, 255
string, ff
string, 11111111
string, 65535
string, 11f71fb04cb
Golang strconv Package »