View Problem

Left Space pad a number

Given the number 73 left pad it to 10 characters "        73"
DiskEdit
ruby
73.to_s.rjust(10)
ExpandDiskEdit
java
private static String spaces(int spaces) {
StringBuffer sb = new StringBuffer();
for(int i=0; i<spaces; i++) {
sb.append(' ');
}
return sb.toString();
}

private static String leftPad(int number, int spaces) {
String numberString = String.valueOf(number);
return spaces(spaces - numberString.length()) + numberString;
}
ExpandDiskEdit
java 1.5 or later
String formatted = String.format("%10d", 73);
DiskEdit
perl
sprintf("%10d", 73);
DiskEdit
groovy
println 73.toString().padLeft(10)
DiskEdit
groovy
printf "%10d\n", 73
ExpandDiskEdit
scala
val formatted = String.format("%10d", int2Integer(73))
ExpandDiskEdit
scala
printf("%10d\n", 73)
DiskEdit
python
"%10s" % 73
ExpandDiskEdit
cpp C++/CLI .NET 2.0
String^ formatted = Convert::ToString(73)->PadLeft(10);
ExpandDiskEdit
cpp C++/CLI .NET 2.0
String^ formatted = String::Format("{0,10:D}", 73);
ExpandDiskEdit
cpp
std::printf("%10d\n", 73);
ExpandDiskEdit
cpp
std::ostringstream os;
os << std::setw(10) << std::setfill(' ') << 73 << std::ends;
std::cout << os.str() << std::endl;
ExpandDiskEdit
cpp
std::cout << boost::format("%|10|") % 73 << std::endl;
ExpandDiskEdit
fsharp
let formatted = sprintf "%10d" 73
printfn "%s" formatted
ExpandDiskEdit
fsharp
let formatted = String.Format("{0,10:D}", 73)
Console.WriteLine(formatted)
ExpandDiskEdit
fsharp
let formatted = Convert.ToString(73).PadLeft(10)
Console.WriteLine(formatted)
ExpandDiskEdit
erlang
Formatted = io_lib:format("~10B", [73]),
ExpandDiskEdit
erlang
io:format("~10B~n", [73]).
DiskEdit
ocaml
Printf.printf "%10d" 73;;
DiskEdit
ocaml
DiskEdit
csharp
public class NumberLeftPadding {
public static void Main() {
string withStringDotFormat = string.Format("{0,10}", 73);
string withToStringDotPadLeft = 73.ToString().PadLeft(10);
}
}
ExpandDiskEdit
php
echo str_pad(73, 10, " ", STR_PAD_LEFT);
ExpandDiskEdit
php
printf("%10d", 73);
DiskEdit
haskell
import Text.Printf

formatted :: String
formatted = printf "%10d" 73
DiskEdit
clojure
(let [s (str 73)
l (count s)]
(str (reduce str (repeat (- 10 l) " ")) s ))
ExpandDiskEdit
fantom
formatted := 73.toStr.padl(10)
ExpandDiskEdit
go
fmt.Printf("%10d", 73)

Submit a new solution for ruby, java, perl, groovy ...