Convert string to decimal number with 2 decimal places in Java
问题 In Java, I am trying to parse a string of format "###.##" to a float. The string should always have 2 decimal places. Even if the String has value 123.00 , the float should also be 123.00 , not 123.0 . This is what I have so far: 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