View Problem

Format a decimal number

Format the number 7/8 as a decimal with 2 places: 0.88
ExpandDiskEdit
fsharp
printfn "%3.2f" (0.7 / 0.8)
ExpandDiskEdit
fsharp
let formatted = String.Format("{0,3:F2}", (0.7 / 0.8))
Console.WriteLine(formatted)
ExpandDiskEdit
fantom
formatted := (7.0/8.0).toLocale("0.00")
ExpandDiskEdit
groovy
def result = 7/8
println result.round(new MathContext(2))
ExpandDiskEdit
groovy
def result = 7/8
printf "%.2g", result
DiskEdit
groovy 1.6.4
new Double(7/8).round(2)
DiskEdit
clojure
(format "%3.2f" (/ 7.0 8))
DiskEdit
clojure
(* 0.01 (Math/round (* 100 (float (/ 7 8)))))
ExpandDiskEdit
erlang
Formatted = io_lib:format("~.2f", [7/8]),
ExpandDiskEdit
erlang
io:format("~.2f~n", [7/8]).

Submit a new solution for fsharp, fantom, groovy, clojure ...
There are 21 other solutions in additional languages (cpp, csharp, go, haskell ...)