Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Go

Idiom #156 Format integer with zero-padding

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

import "fmt"
s := fmt.Sprintf("%03d", i)

Flag 0 means "pad with zeroes, not with spaces".
Flag 3 is the width.
(def s (format "%03d" i))

New implementation...
< >
programming-idioms.org