title: Java BigDecimal Rounding
body: <p>I am learning BigDecimal and i want it to retrieve the exact number i entered, the following code is rouding the number and i dont know why</p>
  
  <pre><code>public static BigDecimal parseFromNumberString(String numberString) {
  
      if (numberString != null) {
  
          String nonSpacedString =
              numberString.replaceAll("[ \\t\\n\\x0B\\f\\r]", "").replaceAll("%", "");
  
          int indexOfComma = nonSpacedString.indexOf(',');
          int indexOfDot = nonSpacedString.indexOf('.');
          NumberFormat format = null;
  
          if (indexOfComma &lt; indexOfDot) {
              nonSpacedString = nonSpacedString.replaceAll("[,]", "");
              format = new DecimalFormat("##.#");
          } else if (indexOfComma &gt; indexOfDot) {
              nonSpacedString = nonSpacedString.replaceAll("[.]", "");    
              DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols();
              otherSymbols.setDecimalSeparator(',');
              format = new DecimalFormat("##,#", otherSymbols);
          } else {
              format = new DecimalFormat();
          }
          try {
              return new BigDecimal(format.parse(nonSpacedString).doubleValue(), new MathContext(12));
          } catch (ParseException e) {
              // unrecognized number format
              return null;
          }
      }
      return null;
  }
  </code></pre>
  
  <p>If i do something like </p>
  
  <pre><code>public static void main(String[] args){
      BigDecimal d = Test.parseFromNumberString("0.39");
      System.out.println(d);  
  }
  </code></pre>
  
  <p>The value printed is 0,00 and not 0.39</p>