Login
|
Signup
langref.org
-
python
,
erlang
,
clojure
,
cpp
...
add..
all
csharp
fantom
go
groovy
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
Format a decimal number
Format the number 7/8 as a decimal with 2 places: 0.88
python
"%.2f" % (7 / 8.0)
"%.2f" % (7 / 8.0)
python
round(7./8., 2)
round(7./8., 2)
erlang
Formatted = io_lib:format("~.2f", [7/8]),
-module(decfmt).
-export([start/0]).
start() ->
Formatted = io_lib:format("~.2f", [7/8]),
io:format("~s~n", [Formatted]).
erlang
io:format("~.2f~n", [7/8]).
-module(decfmt).
-export([start/0]).
start() ->
io:format("~.2f~n", [7/8]).
clojure
(format "%3.2f" (/ 7.0 8))
(format "%3.2f" (/ 7.0 8))
clojure
(* 0.01 (Math/round (* 100 (float (/ 7 8)))))
(* 0.01 (Math/round (* 100 (float (/ 7 8)))))
cpp
C++/CLI .NET 2.0
String^ formatted = String::Format("{0,3:F2}", result);
using namespace System;
int main()
{
double result = 7. / 8.;
String^ formatted = String::Format("{0,3:F2}", result);
Console::WriteLine(formatted);
}
cpp
C++/CLI .NET 2.0
Console::WriteLine("{0,3:F2}", (7. / 8.));
using namespace System;
int main()
{
Console::WriteLine("{0,3:F2}", (7. / 8.));
}
cpp
std::printf("%3.2f\n", result);
#include <cstdio>
int main()
{
double result = 7. / 8.;
std::printf("%3.2f\n", result);
}
cpp
std::ostringstream os;
os.width(3); os.fill('0'); os.setf(std::ios::fixed|std::ios::showpoint); os.precision(2);
os << result << std::ends;
std::cout << os.str() << std::endl;
#include <iostream>
#include <sstream>
#include <iomanip>
int main()
{
double result = 7. / 8.;
std::ostringstream os;
os.width(3); os.fill('0'); os.setf(std::ios::fixed|std::ios::showpoint); os.precision(2);
os << result << std::ends;
std::cout << os.str() << std::endl;
}
cpp
std::cout << boost::format("%|3.2f|") % result << std::endl;
#include <iostream>
#include "boost/format.hpp"
int main()
{
double result = 7. / 8.;
std::cout << boost::format("%|3.2f|") % result << std::endl;
}
fsharp
printfn "%3.2f" (0.7 / 0.8)
#light
open System
printfn "%3.2f" (0.7 / 0.8)
fsharp
let formatted = String.Format("{0,3:F2}", (0.7 / 0.8))
Console.WriteLine(formatted)
#light
open System
let formatted = String.Format("{0,3:F2}", (0.7 / 0.8))
Console.WriteLine(formatted)
Submit a new solution for
python
,
erlang
,
clojure
,
cpp
...
There are 18 other solutions in
additional
languages (
csharp
,
fantom
,
go
,
groovy
...)