Login
|
Signup
langref.org
-
python
,
clojure
, and
cpp
add..
all
csharp
erlang
fantom
fsharp
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)
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;
}
Submit a new solution for
python
,
clojure
, or
cpp
There are 22 other solutions in
additional
languages (
csharp
,
erlang
,
fantom
,
fsharp
...)