title: Convert string to decimal number with 2 decimal places in Java
body: <p>In Java, I am trying to parse a string of format <code>"###.##"</code> to a float.  The string should always have 2 decimal places.</p>
  
  <p>Even if the String has value <code>123.00</code>, the float should also be <code>123.00</code>, not <code>123.0</code>.</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>System.out.println("string liters of petrol putting in preferences is " + stringLitersOfPetrol);
  
  Float litersOfPetrol = Float.parseFloat(stringLitersOfPetrol);
  
  DecimalFormat df = new DecimalFormat("0.00");
  df.setMaximumFractionDigits(2);
  
  litersOfPetrol = Float.parseFloat(df.format(litersOfPetrol));
  
  System.out.println("liters of petrol before putting in editor: " + litersOfPetrol);
  </code></pre>
  
  <p>It prints:</p>
  
  <pre><code>string liters of petrol putting in preferences is 010.00 
  liters of petrol before putting in editor: 10.0
  </code></pre>