Login
|
Signup
langref.org
-
python
,
fsharp
,
fantom
,
groovy
...
add..
all
clojure
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
cpp
C++/CLI .NET 2.0
String^ formatted = Convert::ToString(42)->PadLeft(8, '0');
using namespace System;
int main()
{
String^ formatted = Convert::ToString(42)->PadLeft(8, '0');
}
cpp
C++/CLI .NET 2.0
String^ formatted = String::Format("{0,8:D8}", 42);
using namespace System;
int main()
{
String^ formatted = String::Format("{0,8:D8}", 42);
}
cpp
std::printf("%08d", 42);
#include <cstdio>
int main()
{
std::printf("%08d", 42);
}
cpp
std::ostringstream os;
os << std::setw(8) << std::setfill('0') << 42 << std::ends;
std::cout << os.str() << std::endl;
#include <iostream>
#include <iomanip>
#include <sstream>
int main()
{
std::ostringstream os;
os << std::setw(8) << std::setfill('0') << 42 << std::ends;
std::cout << os.str() << std::endl;
}
cpp
std::cout << boost::format("%|08|") % 42 << std::endl;
#include "boost/format.hpp"
int main()
{
std::cout << boost::format("%|08|") % 42 << std::endl;
}
Submit a new solution for
python
,
fsharp
,
fantom
,
groovy
...
There are 20 other solutions in
additional
languages (
clojure
,
csharp
,
erlang
,
go
...)