Lo que todo programador debería saber sobre aritmética de punto flotante
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

added language details

+25 -14
+4 -1
content/languages/csharp.html
··· 19 19 20 20 How to Round 21 21 ------------ 22 - TODO 22 + The <code>Math.Round()</code> method works with the double and decimal types, and allows you to specify a [rounding mode](/errors/rounding/): 23 + 24 + Math.Round(1.25m, 1, MidpointRounding.AwayFromZero); // returns 1.3 25 + 23 26 24 27 25 28 Resources
+12 -10
content/languages/java.html
··· 12 12 Decimal Types 13 13 ------------- 14 14 Java has an [arbitrary-precision](/formats/exact/) decimal type named <code>java.math.BigDecimal</code>, which 15 - also allows to choose the rounding mode. 16 - BigDecimal a = new BigDecimal("0.1"); 17 - BigDecimal b = new BigDecimal("0.2"); 18 - BigDecimal c = a.add(b); // returns a BigDecimal representing exactly 0.3 15 + also allows to choose the [rounding mode](/errors/rounding/). 16 + BigDecimal a = new BigDecimal("0.1"); 17 + BigDecimal b = new BigDecimal("0.2"); 18 + BigDecimal c = a.add(b); // returns a BigDecimal representing exactly 0.3 19 19 20 20 21 21 How to Round 22 22 ------------ 23 23 To get a String: 24 - String.format("%.2f", 1.2399) // returns "1.24" 25 - String.format("%.3f", 1.2399) // returns "1.240" 26 - String.format("%.2f", 1.2) // returns "1.20" 24 + String.format("%.2f", 1.2399) // returns "1.24" 25 + String.format("%.3f", 1.2399) // returns "1.240" 26 + String.format("%.2f", 1.2) // returns "1.20" 27 27 To print to standard output (or any <code>PrintStream</code>): 28 - System.out.printf("%.2f", 1.2399) // same syntax as String.format() 28 + System.out.printf("%.2f", 1.2399) // same syntax as String.format() 29 29 If you don't want trailing zeroes: 30 - new DecimalFormat("0.00").format(1.2)// returns "1.20" 31 - new DecimalFormat("0.##").format(1.2)// returns "1.2" 30 + new DecimalFormat("0.00").format(1.2)// returns "1.20" 31 + new DecimalFormat("0.##").format(1.2)// returns "1.2" 32 + If you need a specific [rounding mode](/errors/rounding/): 33 + new BigDecimal("1.25").setScale(1, RoundingMode.HALF_EVEN); // returns 1.2 32 34 33 35 34 36 Resources
+9 -3
content/languages/javascript.html
··· 11 11 Decimal Types 12 12 ------------- 13 13 14 - TODO: is there a standard or widely used JS bignum/decimal library? 14 + 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/): 15 + var a = new BigDecimal("0.01"); 16 + var b = new BigDecimal("0.02"); 17 + var c = a.add(b); // 0.03 18 + var d = c.setScale(1, BigDecimal.prototype.ROUND_HALF_UP); 19 + 15 20 16 21 How to Round 17 22 ------------ ··· 19 24 num.toPrecision(1) //returns 5 as string 20 25 num.toPrecision(2) //returns 5.1 as string 21 26 num.toPrecision(4) //returns 5.123 as string 22 - 23 - TODO: talk about rounding modes; Firefox JavaScript seems to use round-half-odd 27 + Using a specific rounding mode: 28 + new BigDecimal("1.25").setScale(1, BigDecimal.prototype.ROUND_HALF_UP); 24 29 25 30 26 31 Resources 27 32 --------- 33 + [BigDecimal for JavaScript](http://stz-ida.de/html/oss/js_bigdecimal.html.en) 28 34 [Core JavaScript Reference](https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference) 29 35 30 36