Logo

Programming-Idioms

History of Idiom 156 > diff from v2 to v3

Edit summary for version 3 by programming-idioms.org:
[Go] Integer variable is i, per idiom statement

Version 2

2017-03-18, 16:32:00

Version 3

2017-03-19, 20:53:48

Idiom #156 Format integer with zero-padding

Assign to string s the value of integer i in 3 decimal digits. Pad with zeros if i < 100. Keep all digits if i1000.

Idiom #156 Format integer with zero-padding

Assign to string s the value of integer i in 3 decimal digits. Pad with zeros if i < 100. Keep all digits if i1000.

Extra Keywords
pad leading fixed width
Extra Keywords
pad leading fixed width
Imports
import "fmt"
Imports
import "fmt"
Code
s := fmt.Sprintf("%03d", x)
Code
s := fmt.Sprintf("%03d", i)
Comments bubble
Flag 0 means "pad with zeroes, not with spaces".
Flag 3 is the width.
Comments bubble
Flag 0 means "pad with zeroes, not with spaces".
Flag 3 is the width.
Doc URL
https://golang.org/pkg/fmt/
Doc URL
https://golang.org/pkg/fmt/
Demo URL
https://play.golang.org/p/wQ-9ZsCIUX
Demo URL
https://play.golang.org/p/iLsDceIiA5