···19192020How to Round
2121------------
2222-TODO
2222+The <code>Math.Round()</code> method works with the double and decimal types, and allows you to specify a [rounding mode](/errors/rounding/):
2323+2424+ Math.Round(1.25m, 1, MidpointRounding.AwayFromZero); // returns 1.3
2525+232624272528Resources
+12-10
content/languages/java.html
···1212Decimal Types
1313-------------
1414Java has an [arbitrary-precision](/formats/exact/) decimal type named <code>java.math.BigDecimal</code>, which
1515-also allows to choose the rounding mode.
1616- BigDecimal a = new BigDecimal("0.1");
1717- BigDecimal b = new BigDecimal("0.2");
1818- BigDecimal c = a.add(b); // returns a BigDecimal representing exactly 0.3
1515+also allows to choose the [rounding mode](/errors/rounding/).
1616+ BigDecimal a = new BigDecimal("0.1");
1717+ BigDecimal b = new BigDecimal("0.2");
1818+ BigDecimal c = a.add(b); // returns a BigDecimal representing exactly 0.3
191920202121How to Round
2222------------
2323To get a String:
2424- String.format("%.2f", 1.2399) // returns "1.24"
2525- String.format("%.3f", 1.2399) // returns "1.240"
2626- String.format("%.2f", 1.2) // returns "1.20"
2424+ String.format("%.2f", 1.2399) // returns "1.24"
2525+ String.format("%.3f", 1.2399) // returns "1.240"
2626+ String.format("%.2f", 1.2) // returns "1.20"
2727To print to standard output (or any <code>PrintStream</code>):
2828- System.out.printf("%.2f", 1.2399) // same syntax as String.format()
2828+ System.out.printf("%.2f", 1.2399) // same syntax as String.format()
2929If you don't want trailing zeroes:
3030- new DecimalFormat("0.00").format(1.2)// returns "1.20"
3131- new DecimalFormat("0.##").format(1.2)// returns "1.2"
3030+ new DecimalFormat("0.00").format(1.2)// returns "1.20"
3131+ new DecimalFormat("0.##").format(1.2)// returns "1.2"
3232+If you need a specific [rounding mode](/errors/rounding/):
3333+ new BigDecimal("1.25").setScale(1, RoundingMode.HALF_EVEN); // returns 1.2
323433353436Resources
+9-3
content/languages/javascript.html
···1111Decimal Types
1212-------------
13131414-TODO: is there a standard or widely used JS bignum/decimal library?
1414+The 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/):
1515+ var a = new BigDecimal("0.01");
1616+ var b = new BigDecimal("0.02");
1717+ var c = a.add(b); // 0.03
1818+ var d = c.setScale(1, BigDecimal.prototype.ROUND_HALF_UP);
1919+15201621How to Round
1722------------
···1924 num.toPrecision(1) //returns 5 as string
2025 num.toPrecision(2) //returns 5.1 as string
2126 num.toPrecision(4) //returns 5.123 as string
2222-2323-TODO: talk about rounding modes; Firefox JavaScript seems to use round-half-odd
2727+Using a specific rounding mode:
2828+ new BigDecimal("1.25").setScale(1, BigDecimal.prototype.ROUND_HALF_UP);
242925302631Resources
2732---------
3333+[BigDecimal for JavaScript](http://stz-ida.de/html/oss/js_bigdecimal.html.en)
2834[Core JavaScript Reference](https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference)
29353036