Login
|
Signup
langref.org
-
ruby
,
csharp
,
clojure
,
cpp
...
add..
all
erlang
fantom
groovy
haskell
java
ocaml
perl
php
python
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
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)
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);
}
}
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)
go
fmt.Printf("%.2f", 7.0 / 8.0)
package main
import "fmt"
func main() {
fmt.Printf("%.2f", 7.0 / 8.0)
}
Submit a new solution for
ruby
,
csharp
,
clojure
,
cpp
...
There are 18 other solutions in
additional
languages (
erlang
,
fantom
,
groovy
,
haskell
...)