Login
|
Signup
langref.org
-
clojure
,
cpp
,
csharp
,
fantom
...
add..
all
erlang
go
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
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;
}
csharp
public class FormatDecimal {
public static void Main() {
decimal result = decimal.Round( 7 / 8m, 2);
System.Console.WriteLine(result);
}
}
public class FormatDecimal {
public static void Main() {
decimal result = decimal.Round( 7 / 8m, 2);
System.Console.WriteLine(result);
}
}
fantom
formatted := (7.0/8.0).toLocale("0.00")
class SolutionXX
{
Void main()
{
formatted := (7.0/8.0).toLocale("0.00")
}
}
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)
groovy
def result = 7/8
println result.round(new MathContext(2))
import java.math.*
def result = 7/8
println result.round(new MathContext(2))
groovy
def result = 7/8
printf "%.2g", result
// run on Java 1.5+
def result = 7/8
printf "%.2g", result
groovy
1.6.4
new Double(7/8).round(2)
new Double(7/8).round(2)
haskell
import Text.Printf
printf "%3.2f" (7/8)
import Text.Printf
printf "%3.2f" (7/8)
haskell
main = putStrLn $ Numeric.showFFloat (Just 2) (7/8) ""
main = putStrLn $ Numeric.showFFloat (Just 2) (7/8) ""
java
1.5 or later
String formatted = String.format("%3.2f", 7./8.);
public class SolutionXX {
public static void main(String[] args) {
String formatted = String.format("%3.2f", 7./8.);
System.out.println(formatted);
}
}
ocaml
Printf.printf "%4.2f" (7. /. 8.);;
Printf.printf "%4.2f" (7. /. 8.);;
ocaml
let s = Printf.sprintf "%4.2f" (7. /. 8.) in
print_string s;;
let s = Printf.sprintf "%4.2f" (7. /. 8.) in
print_string s;;
perl
sprintf("%.2f", 7/8);
sprintf("%.2f", 7/8);
php
printf("%.2g", 7/8);
<?php
printf("%.2g", 7/8);
?>
php
echo round(7/8, 2);
<?php
echo round(7/8, 2);
?>
python
"%.2f" % (7 / 8.0)
"%.2f" % (7 / 8.0)
python
round(7./8., 2)
round(7./8., 2)
ruby
(7.0/8.0*100).round/100.0
(7.0/8.0*100).round/100.0
ruby
1.9+
(7.0/8.0).round(2)
(7.0/8.0).round(2)
scala
val formatted = String.format("%3.2f", double2Double(7./8.))
object SolutionXX extends Application {
val formatted = String.format("%3.2f", double2Double(7./8.))
println(formatted)
}
scala
printf("%3.2f\n", 7./8.)
object SolutionXX extends Application {
printf("%3.2f\n", 7./8.)
}
Submit a new solution for
clojure
,
cpp
,
csharp
,
fantom
...
There are 3 other solutions in
additional
languages (
erlang
or
go
)