Login
|
Signup
langref.org
-
ruby
,
java
,
perl
,
groovy
...
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
ruby
(7.0/8.0*100).round/100.0
(7.0/8.0*100).round/100.0
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);
}
}
perl
sprintf("%.2f", 7/8);
sprintf("%.2f", 7/8);
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)
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.)
}
python
"%.2f" % (7 / 8.0)
"%.2f" % (7 / 8.0)
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)
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]).
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;;
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);
}
}
php
printf("%.2g", 7/8);
<?php
printf("%.2g", 7/8);
?>
Submit a new solution for
ruby
,
java
,
perl
,
groovy
...