Login
|
Signup
langref.org
-
python
,
fsharp
,
fantom
,
groovy
...
add..
all
cpp
csharp
erlang
go
haskell
java
ocaml
perl
php
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
python
"%08d" % 42
"%08d" % 42
fsharp
printfn "%08d" 42
#light
open System
printfn "%08d" 42
fsharp
let formatted = sprintf "%08d" 42
printfn "%s" formatted
#light
open System
let formatted = sprintf "%08d" 42
printfn "%s" formatted
fsharp
let buffer = new StringBuilder()
Printf.bprintf buffer "%08d" 42
printfn "%s" (buffer.ToString())
#light
open System
open System.Text
let buffer = new StringBuilder()
Printf.bprintf buffer "%08d" 42
printfn "%s" (buffer.ToString())
fsharp
let formatted = String.Format("{0,8:D8}", 42)
Console.WriteLine(formatted)
#light
open System
let formatted = String.Format("{0,8:D8}", 42)
Console.WriteLine(formatted)
fsharp
let formatted = Convert.ToString(42).PadLeft(8, '0')
Console.WriteLine(formatted)
#light
open System
let formatted = Convert.ToString(42).PadLeft(8, '0')
Console.WriteLine(formatted)
fantom
formatted := 42.toStr.padl(8, '0')
class SolutionXX
{
Void main()
{
formatted := 42.toStr.padl(8, '0')
}
}
fantom
formatted := 42.toLocale("00000000")
class SolutionXX
{
Void main()
{
formatted := 42.toLocale("00000000")
}
}
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
clojure
clojure
(defn pad
([x] (if (> 8 (.length (str x))) (pad (str 0 x)) (str x)))
)
(defn pad
([x] (if (> 8 (.length (str x))) (pad (str 0 x)) (str x)))
)
clojure
clojure
(defn pad [x]
(format "%08d" x))
(defn pad [x]
(format "%08d" x))
clojure
(format "%08d" 42)
(format "%08d" 42)
Submit a new solution for
python
,
fsharp
,
fantom
,
groovy
...
There are 22 other solutions in
additional
languages (
cpp
,
csharp
,
erlang
,
go
...)