<p>Recently I tried understanding the use of <strong>java.math.MathContext</strong> but failed to understand properly. Is it used for rounding in <code>java.math.BigDecimal</code>. If yes why does not it round the decimal digits but even mantissa part.</p>

<p>From API docs, I came to know that it follows the standard specified in <code>ANSI X3.274-1996</code> and <code>ANSI X3.274-1996/AM 1-2000</code> specifications but I did not get them to read online.</p>

<p>Please let me know if you have any idea on this.</p>

[{'answer_id': 7550, 'author': 'Derek Park', 'author_id': 872, 'author_profile': 'https://Stackoverflow.com/users/872', 'pm_score': 3, 'selected': False, 'text': '<p>If I\'m understanding you correctly, it sounds like you\'re expecting the MathContext to control how many digits should be kept after the decimal point.  That\'s not what it\'s for.  It specifies how many digits to keep, <em>total</em>.  So if you specify that you want 3 significant digits, that\'s all you\'re going to get.</p>\n\n<p>For example, this:</p>\n\n<pre><code>System.out.println(new BigDecimal("1234567890.123456789",\n                   new MathContext(20)));\n\nSystem.out.println(new BigDecimal("1234567890.123456789",\n                   new MathContext(10)));\n\nSystem.out.println(new BigDecimal("1234567890.123456789",\n                   new MathContext(5)));\n</code></pre>\n\n<p>will output:</p>\n\n<pre><code>1234567890.123456789\n1234567890\n1.2346E+9\n</code></pre>\n'}, {'answer_id': 7552, 'author': 'jatanp', 'author_id': 959, 'author_profile': 'https://Stackoverflow.com/users/959', 'pm_score': 3, 'selected': False, 'text': "<p>It's not for fun. Actually I found some online example, which stated the use of <code>MathContext</code> to round the amounts/numbers stored in BigDecimal.</p>\n\n<p>For example,</p>\n\n<p>If <code>MathContext</code> is configured to have <code>precision = 2</code> and <code>rounding mode = ROUND_HALF_EVEN</code></p>\n\n<p><code>BigDecimal Number = 0.5294</code>, is <em>rounded</em> to <strong>0.53</strong></p>\n\n<p>So I thought it is a newer technique and used it for rounding purpose. However it turned into nightmare because it started rounding even mentissa part of number.</p>\n\n<p>For example,</p>\n\n<p><code>Number = 1.5294</code> is rounded to <code>1.5</code></p>\n\n<p><code>Number = 10.5294</code> is rounded to <code>10</code></p>\n\n<p><code>Number = 101.5294</code> is rounded to <code>100</code> </p>\n\n<p>.... and so on</p>\n\n<p>So this is not the behavior I expected for rounding (as precision = 2).</p>\n\n<p>It seems to be having some logic because from patter I can say that it takes first two digits (as precision is 2) of number and then appends 0's till the no. of digits become same as unrounded amount (checkout the example of 101.5294 ...)</p>\n"}, {'answer_id': 7561, 'author': 'Derek Park', 'author_id': 872, 'author_profile': 'https://Stackoverflow.com/users/872', 'pm_score': 7, 'selected': True, 'text': '<p>@jatan</p>\n\n<blockquote>\n  <p>Thanks for you answer. It makes sense. Can you please explain me MathContext in the context of BigDecimal#round method.</p>\n</blockquote>\n\n<p>There\'s nothing special about <code>BigDecimal.round()</code> <em>vs.</em> any other <code>BigDecimal</code> method.  In all cases, the <code>MathContext</code> specifies the number of significant digits and the rounding technique.  Basically, there are two parts of every <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/math/MathContext.html" rel="noreferrer"><code>MathContext</code></a>.  There\'s a precision, and there\'s also a <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/math/RoundingMode.html" rel="noreferrer"><code>RoundingMode</code></a>.</p>\n\n<p>The precision again specifies the number of significant digits.  So if you specify <code>123</code> as a number, and ask for 2 significant digits, you\'re going to get <code>120</code>.  It might be clearer if you think in terms of scientific notation.</p>\n\n<p><code>123</code> would be <code>1.23e2</code> in scientific notation.  If you only keep 2 significant digits, then you get <code>1.2e2</code>, or <code>120</code>.  By reducing the number of significant digits, we reduce the precision with which we can specify a number.</p>\n\n<p>The <code>RoundingMode</code> part specifies how we should handle the loss of precision.  To reuse the example, if you use <code>123</code> as the number, and ask for 2 significant digits, you\'ve reduced your precision.  With a <code>RoundingMode</code> of <code>HALF_UP</code> (the default mode), <code>123</code> will become <code>120</code>.  With a <code>RoundingMode</code> of <code>CEILING</code>, you\'ll get <code>130</code>.</p>\n\n<p>For example:</p>\n\n<pre><code>System.out.println(new BigDecimal("123.4",\n                   new MathContext(4,RoundingMode.HALF_UP)));\nSystem.out.println(new BigDecimal("123.4",\n                   new MathContext(2,RoundingMode.HALF_UP)));\nSystem.out.println(new BigDecimal("123.4",\n                   new MathContext(2,RoundingMode.CEILING)));\nSystem.out.println(new BigDecimal("123.4",\n                   new MathContext(1,RoundingMode.CEILING)));\n</code></pre>\n\n<p>Outputs:</p>\n\n<pre><code>123.4\n1.2E+2\n1.3E+2\n2E+2\n</code></pre>\n\n<p>You can see that both the precision and the rounding mode affect the output.</p>\n'}, {'answer_id': 4194330, 'author': 'Øystein Øvrebø', 'author_id': 374167, 'author_profile': 'https://Stackoverflow.com/users/374167', 'pm_score': 6, 'selected': False, 'text': '<p>For rounding just the fractional part of a BigDecimal, check out the <code>BigDecimal.setScale(int newScale, int roundingMode)</code> method.</p>\n\n<p>E.g. to change a number with three digits after the decimal point to one with two digits, and rounding up:</p>\n\n<pre><code>BigDecimal original = new BigDecimal("1.235");\nBigDecimal scaled = original.setScale(2, BigDecimal.ROUND_HALF_UP);\n</code></pre>\n\n<p>The result of this is a BigDecimal with the value 1.24 (because of the rounding up rule)</p>\n'}, {'answer_id': 22988977, 'author': 'radekEm', 'author_id': 1534456, 'author_profile': 'https://Stackoverflow.com/users/1534456', 'pm_score': 4, 'selected': False, 'text': '<p>I would add here, a few examples. I haven\'t found them in previous answers, but I find them useful for those who maybe mislead <strong>significant digits</strong> with number of <strong>decimal places</strong>. Let\'s assume, we have such context:</p>\n\n<pre><code>MathContext MATH_CTX = new MathContext(3, RoundingMode.HALF_UP);\n</code></pre>\n\n<p>For this code:</p>\n\n<pre><code>BigDecimal d1 = new BigDecimal(1234.4, MATH_CTX);\nSystem.out.println(d1);\n</code></pre>\n\n<p>it\'s perfectly clear, that your result is <code>1.23E+3</code> as guys said above. First significant digits are 123...</p>\n\n<p>But what in this case:</p>\n\n<pre><code>BigDecimal d2 = new BigDecimal(0.000000454770054, MATH_CTX);\nSystem.out.println(d2);\n</code></pre>\n\n<p>your number <strong>will not be rounded to 3 places after comma</strong> - for someone it can be not intuitive and worth to emphasize. Instead it will be rounded to the <strong>first 3 significant digits</strong>, which in this case are "4 5 4". So above code results in <code>4.55E-7</code> and not in <code>0.000</code> as someone could expect.</p>\n\n<p>Similar examples:</p>\n\n<pre><code>BigDecimal d3 = new BigDecimal(0.001000045477, MATH_CTX);\n System.out.println(d3);  // 0.00100\n\nBigDecimal d4 = new BigDecimal(0.200000477, MATH_CTX);\n System.out.println(d4);   // 0.200\n\nBigDecimal d5 = new BigDecimal(0.000000004, MATH_CTX);\n    System.out.println(d5); //4.00E-9\n</code></pre>\n\n<p>I hope this obvious, but relevant example would be helpful...</p>\n'}]
['https://Stackoverflow.com/questions/7539', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/959/']