How can I format an int in go to make sure there is always two digits?
For example, 1 would be formatted to 01
.
Asked
Active
Viewed 2.4k times
3 Answers
26
You can use fmt.Printf()
or fmt.Sprintf()
to create a string with left-padded zeroes. fmt.Printf()
will print the data while fmt.Sprintf()
will allow you to assign the resulting string to a variable.
Here are the signatures from the docs:
func Printf(format string, a ...interface{}) (n int, err error)
func Sprintf(format string, a ...interface{}) string
For example:
// Printing directly using fmt.Printf()
fmt.Printf("%02d\n", 1)
// With output assignment
count, err := fmt.Printf("%02d\n", 1)
if err == nil {
fmt.Printf("Printed %v bytes\n", count)
} else {
fmt.Println("Error printing")
}
// Assigning to variable using fmt.Sprintf()
formatted := fmt.Sprintf("%02d", 1)
fmt.Println(formatted)

Grokify
- 15,092
- 6
- 60
- 81
-
Thankyou ! It works but is there anyway I could to this without assigning it to a variable. So that if I need to concatenate some strings together I won't have to create thousands of variable. – AlexB Jun 20 '15 at 16:40
-
I get this error : multiple-value fmt.Printf() in single-value context if I don't do this : x, _ = fmt.Sprintf("%02d", 1) – AlexB Jun 20 '15 at 16:42
-
I updated the answer to include example code for both printing directly and assigning to a variable. – Grokify Jun 20 '15 at 16:42
-
`fmt.Printf()` will return an integer and and error. The integer represents the number of bytes printed. If you want to capture that, you need to account for both the byte count and the error. – Grokify Jun 20 '15 at 16:44
-
I still get the same error when trying to print directly like this : fmt.Printf("%02d", rand.Intn(12)) – AlexB Jun 20 '15 at 16:46
-
The following works for me `fmt.Printf("%02d\n", rand.Intn(12))` using `math/rand`. If you are assigning the results to variables you need to do something like `byteCount, err := fmt.Printf("%02d\n", rand.Intn(12))` or `byteCount, _ := fmt.Printf("%02d\n", rand.Intn(12))` to ignore the error. – Grokify Jun 20 '15 at 16:48
-
Thankyou for your complete answer – AlexB Jun 20 '15 at 16:56
10
You should take a look at the fmt.Printf
documentation. It explains all of the formatting flags. The specific ones you are looking for are 0
and 2
. 0
indicates that you want to pad the number to the specified width with leading zeros. 2
indicates the width that you want, these two flags together will pad single digits with a leading 0, but ignore numbers that are 2 digits in length or greater.
package main
import "fmt"
func main() {
for i := 1; i <= 10; i++ {
fmt.Printf("%02d\n", i)
}
}
outputs:
01
02
03
04
05
06
07
08
09
10

Hunter McMillen
- 59,865
- 24
- 119
- 170
-
An error tells me I need to assign the value to a variable : multiple-value fmt.Printf() in single-value context – AlexB Jun 20 '15 at 16:38
-
-
2
Another option:
package main
import (
"golang.org/x/text/language"
"golang.org/x/text/message"
"golang.org/x/text/number"
)
var fmt = message.NewPrinter(language.English)
func main() {
n := number.Decimal(
1, number.Pad('0'), number.FormatWidth(2),
)
fmt.Println(n)
}

Zombo
- 1
- 62
- 391
- 407