title: Format Bigdecimal in Java
body: <p>I would like to format a BigDecimal value according to the following rules:</p>
  
  <pre><code>25 =&gt; 25
  25,1 =&gt; 25,10
  25,10 =&gt; 25,10
  25,12 = &gt; 25,12
  </code></pre>
  
  <p>I have searched the forums but not found a matching question (<a href="https://stackoverflow.com/questions/10269045/format-a-bigdecimal-as-string-with-max-2-decimal-digits-removing-0-on-decimal-p">here</a>, <a href="https://stackoverflow.com/questions/3395825/how-to-print-formatted-bigdecimal-values">here</a> and <a href="https://stackoverflow.com/questions/11858485/formatting-a-number-with-correct-decimal-scale">here</a>) and I have looked at the javadoc for <a href="http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html" rel="nofollow noreferrer">BigDecimal</a> and <a href="http://docs.oracle.com/javase/6/docs/api/java/text/NumberFormat.html" rel="nofollow noreferrer">NumberFormat</a> without understanding how to do this. </p>
  
  <p>EDIT:
  Today I do:</p>
  
  <pre><code>NumberFormat currencyFormat02 = NumberFormat.getCurrencyInstance(locale);
  currencyFormat02.setMinimumFractionDigits(0);
  currencyFormat02.setMaximumFractionDigits(2);
  currencyFormat02.setGroupingUsed(false);
  BigDecimal bd = new BigDecimal("25 or 25,1 or 25,10 or 25,12");
  String x =currencyFormat02.format(bd);
  </code></pre>
  
  <p>x should print as above but does not. </p>