Login
|
Signup
langref.org
-
groovy
add..
all
clojure
cpp
csharp
erlang
fantom
fsharp
go
haskell
java
ocaml
perl
php
python
ruby
scala
Home
All
Solved
Unsolved
Strings
Numbers
Regex
Lists
Maps
Structure
Files
Dates
OOP
Networking
XML
Algorithms
Misc
Parallel
View Problem
Numbers
Output
Zero pad a number
Given the number 42, pad it to 8 characters like 00000042
groovy
formatted = new DecimalFormat('00000000').format(42)
import java.text.DecimalFormat
formatted = new DecimalFormat('00000000').format(42)
assert '00000042' == formatted
groovy
formatted = 42.toString().padLeft(8, '0')
formatted = 42.toString().padLeft(8, '0')
assert '00000042' == formatted
groovy
// to stdout
printf "%08d\n", 42
// to a string
formatted = sprintf("%08d", 42)
// to stdout
printf "%08d\n", 42
// to a string
formatted = sprintf("%08d", 42)
assert formatted == '00000042'
groovy
formatted = String.format("%08d", 42)
formatted = String.format("%08d", 42)
assert '00000042' == formatted
Submit a new solution for
groovy
There are 33 other solutions in
additional
languages (
clojure
,
cpp
,
csharp
,
erlang
...)