title: How to round a number in Java
body: <p>I need the following results:</p>
  
  <pre><code>10.17111 -&gt; 10.17
  10.17445 -&gt; 10.18
  </code></pre>
  
  <p>I tried BigDecimal and DecimalFormat methods and RoundingMode class:</p>
  
  <pre><code>String value = "10.17111";
  BigDecimal bd = new BigDecimal(value);
  BigDecimal bdResult = bd.setScale(2, RoundingMode.UP);
  String result = bdResult.toString();
  System.out.println(result);
  
  Print out = 10.18
  Should be = 10.17
  
  double ddd = 10.17111;
  DecimalFormat d = new DecimalFormat("#.##");
  d.setRoundingMode(RoundingMode.UP);
  String outputResult = d.format(ddd).replace(',', '.');
  System.out.println(outputResult);
  
  Print out = 10.18
  Should be = 10.17
  </code></pre>
  
  <p>And with BigDecimal:</p>
  
  <pre><code>String value2 = "10.17445";
  Print out = 10.18
  As I expected
  </code></pre>
  
  <p>And with DecimalFormat:</p>
  
  <pre><code>double ddd2 = 10.17445;
  Print out = 10.17
  Should be = 10.18
  </code></pre>