View Problem

Zero pad a number

Given the number 42, pad it to 8 characters like 00000042
ExpandDiskEdit
groovy
formatted = new DecimalFormat('00000000').format(42)
ExpandDiskEdit
groovy
formatted = 42.toString().padLeft(8, '0')
ExpandDiskEdit
groovy
// to stdout
printf "%08d\n", 42
// to a string
formatted = sprintf("%08d", 42)
ExpandDiskEdit
groovy
formatted = String.format("%08d", 42)

Submit a new solution for groovy
There are 33 other solutions in additional languages (clojure, cpp, csharp, erlang ...)