···66Floating-Point Types
77--------
88C# has [IEEE 754](/formats/fp/) single and double precision types supported by keywords:
99+910 float f = 0.1f; // 32 bit float, note f suffix
1011 double d = 0.1d; // 64 bit float, suffix optional
1112···1415-------------
1516C# has a 128 bit [limited-precision](/formats/exact/) decimal type denoted by the keyword
1617<code>decimal</code>:
1818+1719 decimal myMoney = 300.1m; // note m suffix on the literal
18201921
+10
content/languages/java.html
···66Floating-Point Types
77--------
88Java has [IEEE 754](/formats/fp/) single and double precision types supported by keywords:
99+910 float f = 0.1f; // 32 bit float, note f suffix
1011 double d = 0.1d; // 64 bit float, suffix optional
1212+1113The `strictfp` keyword on classes, interfaces and methods forces all intermediate results of floating-point calculations to be IEEE 754 values as well, guaranteeing identical results on all platforms. Without that keyword, implementations can use an extended exponent range where available, resulting in more precise results and faster execution on many common CPUs.
12141315Decimal Types
1416-------------
1517Java has an [arbitrary-precision](/formats/exact/) decimal type named <code>java.math.BigDecimal</code>, which
1618also allows to choose the [rounding mode](/errors/rounding/).
1919+1720 BigDecimal a = new BigDecimal("0.1");
1821 BigDecimal b = new BigDecimal("0.2");
1922 BigDecimal c = a.add(b); // returns a BigDecimal representing exactly 0.3
···2225How to Round
2326------------
2427To get a String:
2828+2529 String.format("%.2f", 1.2399) // returns "1.24"
2630 String.format("%.3f", 1.2399) // returns "1.240"
2731 String.format("%.2f", 1.2) // returns "1.20"
3232+2833To print to standard output (or any <code>PrintStream</code>):
3434+2935 System.out.printf("%.2f", 1.2399) // same syntax as String.format()
3636+3037If you don't want trailing zeroes:
3838+3139 new DecimalFormat("0.00").format(1.2)// returns "1.20"
3240 new DecimalFormat("0.##").format(1.2)// returns "1.2"
4141+3342If you need a specific [rounding mode](/errors/rounding/):
4343+3444 new BigDecimal("1.25").setScale(1, RoundingMode.HALF_EVEN); // returns 1.2
35453646
+5
content/languages/javascript.html
···66Floating-Point Types
77--------
88JavaScript is dynamically typed and will often convert implicitly between strings and floating-point numbers (which are IEEE 64 bit values). To force a variable to floating-point, use the global <code>parseFloat()</code> function.
99+910 var num = parseFloat("3.5");
10111112Decimal Types
1213-------------
13141415The best decimal type for JavaScript seems to be a port of [Java's](/languages/java/) <code>BigDecimal</code> class, which also supports [rounding modes](/errors/rounding/):
1616+1517 var a = new BigDecimal("0.01");
1618 var b = new BigDecimal("0.02");
1719 var c = a.add(b); // 0.03
···20222123How to Round
2224------------
2525+2326 var num = 5.123456;
2427 num.toPrecision(1) //returns 5 as string
2528 num.toPrecision(2) //returns 5.1 as string
2629 num.toPrecision(4) //returns 5.123 as string
3030+2731Using a specific rounding mode:
3232+2833 new BigDecimal("1.25").setScale(1, BigDecimal.prototype.ROUND_HALF_UP);
29343035
+11
content/languages/perl.html
···1212Perl can also store decimal numbers as strings, but the builtin arithmetic operators will convert them to integer or floating-point values to perform the operation.
13131414The Math::BigFloat extension provides an arbitrary-precision [decimal type](/formats/exact/):
1515+1516 use Math::BigFloat ':constant'
1617 my $f = 0.1 + 0.2; # returns exactly 0.3
1818+1719The Number::Fraction extension provides a fraction type that overloads the arithmetic operators with [symbolic](/formats/exact/) fraction arithmetic:
2020+1821 use Number::Fraction ':constants';
1922 my $f = '1/2' - '1/3'; # returns 1/6
2323+2024The Math::BigRat extension provides similar functionality. Its advantage is compatibility with the Math::BigInt and Math::BigFloat extensions, but it does not seem to support fraction literals.
21252226How to Round
2327------------
2428To get a string:
2929+2530 $result = sprintf("%.2f", 1.2345); # returns 1.23
3131+2632To format output:
3333+2734 printf("%.2f", 1.2); # prints 1.20
3535+2836Note that this implicitly uses [round-to-even](/errors/rounding/). The variable <code>$#</code> contains the default format for printing numbers, but its use is considered deprecated.
29373038The Math::Round extension provides various functions for rounding floating-point values:
3939+3140 use Math::Round qw(:all);
3241 $result = nearest(.1, 4.567) # prints 4.6
3342 $result = nearest(.01, 4.567) # prints 4.57
4343+3444The Math::BigFloat extension also supports various [rounding modes](/errors/rounding/):
4545+3546 use Math::BigFloat;
3647 my $n = Math::BigFloat->new(123.455);
3748 my $f1 = $n->round('','-2','common'); # returns 123.46
+3
content/languages/php.html
···66Floating-Point Types
77--------
88PHP is dynamically typed and will often convert implicitly between strings and floating-point numbers (which are platform-dependant, but typically IEEE 64 bit values). To force a value to floating-point, evaluate it in a numerical context:
99+910 $foo = 0 + "10.5";
101111121213Decimal Types
1314-------------
1415The BC Math extension implements [arbitrary-precision](/formats/exact/) decimal math:
1616+1517 $a = '0.1';
1618 $b = '0.2';
1719 echo bcadd($a, $b); // prints 0.3
···1921How to Round
2022------------
2123Rounding can be done with the `number_format()` function:
2424+2225 $number = 4.123;
2326 echo number_format($number, 2); // prints 4.12
2427
+8-2
content/languages/python.html
···77--------
88Almost all platforms map Python floats to [IEEE 754](/formats/fp/)
99double precision.
1010+1011 f = 0.1
11121213Decimal Types
1314-------------
1414-Python has an [arbitrary-precision](/formats/exact/) decimal type named <code>Decimal</code> in the <code>decimal</code> module, which
1515-also allows to choose the [rounding mode](/errors/rounding/).
1515+Python has an [arbitrary-precision](/formats/exact/) decimal type named <code>Decimal</code> in the <code>decimal</code> module, which also allows to choose the [rounding mode](/errors/rounding/).
1616+1617 a = Decimal('0.1')
1718 b = Decimal('0.2')
1819 c = a + b # returns a Decimal representing exactly 0.3
···2021How to Round
2122------------
2223To get a string:
2424+2325 "%.2f" % 1.2399 # returns "1.24"
2426 "%.3f" % 1.2399 # returns "1.240"
2527 "%.2f" % 1.2 # returns "1.20"
2828+2629To print to standard output:
3030+2731 print "%.2f" % 1.2399 # just use print and string formatting
3232+2833Specific [rounding modes](/errors/rounding/) and other parameters can be defined in a Context object:
3434+2935 getcontext().prec = 7
30363137Resources