View Problem

Right Space pad a number

Given the number 1024 right pad it to 6 characters "1024  "
DiskEdit
ruby
1024.to_s.ljust(6)
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 rightPad(int number, int spaces) {
String numberString = String.valueOf(number);
return numberString + spaces(spaces - numberString.length());
}
ExpandDiskEdit
java org.apache.commons
String text = StringUtils.rightPad(String.valueOf(1024), 6)
ExpandDiskEdit
java 1.5 or later
String formatted = String.format("%-6d", 1024);
DiskEdit
perl
sprintf("%-6d", 1024);
DiskEdit
groovy
println 1024.toString().padRight(6)
DiskEdit
groovy
formatted = sprintf("%-6d", 1024)
ExpandDiskEdit
scala
val formatted = String.format("%-6d", int2Integer(1024))
ExpandDiskEdit
scala
printf("%-6d\n", 1024)
ExpandDiskEdit
scala 2.9.0.1
println("%-6d".format(1024))
DiskEdit
python
"%-6s" % 1024
DiskEdit
python
str(1024).rjust(6)
DiskEdit
python 2.6 or later
'{0: <6}'.format(1024)
ExpandDiskEdit
cpp C++/CLI .NET 2.0
String^ formatted = Convert::ToString(1024)->PadRight(6);
ExpandDiskEdit
cpp C++/CLI .NET 2.0
String^ formatted = String::Format("{0,-6:D}", 1024);
ExpandDiskEdit
cpp
std::printf("%-6d\n", 1024);
ExpandDiskEdit
cpp
std::ostringstream os;
os << std::setw(6) << std::setfill(' ') << std::left << 1024 << std::ends;
std::cout << os.str() << std::endl;
ExpandDiskEdit
cpp
std::cout << boost::format("%|-6|") % 1024 << std::endl;
ExpandDiskEdit
fsharp
printfn "%-6d" 1024
ExpandDiskEdit
fsharp
let formatted = String.Format("{0,-6:D}", 1024)
Console.WriteLine(formatted)
ExpandDiskEdit
fsharp
let formatted = Convert.ToString(1024).PadRight(6)
Console.WriteLine(formatted)
ExpandDiskEdit
erlang
Formatted = io_lib:format("~-6B", [1024]),
ExpandDiskEdit
erlang
io:format("~-6B~n", [1024]).
DiskEdit
ocaml
Printf.printf "%-6i" 1024;;
DiskEdit
csharp
public class NumberRightPadding {
public static void Main() {
string withStringDotFormat = string.Format("{0,-6}", 1024);
string withToStringDotPadRight = 1024.ToString().PadRight(6);
}
}
ExpandDiskEdit
php
echo str_pad(1024, 6, " ");
ExpandDiskEdit
php
printf("%s ", 1024);
DiskEdit
haskell
let s = show 1024
p = 6
in s ++ (replicate (p - length s) ' ')
DiskEdit
haskell
import Text.Printf

main = do
putStrLn $ printf "%-6d" (1024::Int)
DiskEdit
clojure
(let [s (str 1024)
l (count s)]
(str s (reduce str (repeat (- 6 l) " "))))
DiskEdit
clojure
(format "%-6d" 1024)
ExpandDiskEdit
fantom
formatted := 1024.toStr.padr(6)
ExpandDiskEdit
go
fmt.Printf("%-6d", 1024)

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