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.parseFloat(df.format(litersOfPetrol));
System.out.println("liters of petrol before putting in editor: " + litersOfPetrol);
It prints:
string liters of petrol putting in preferences is 010.00
liters of petrol before putting in editor: 10.0