This line is your problem:
litersOfPetrol = Float.parseFloat(df.format(litersOfPetrol));
There you formatted your float to a string as you wanted, but then that string was transformed into a float again, and then what you printed in stdout was your float, which got standard formatting. Take a look at this code
import java.text.DecimalFormat; String stringLitersOfPetrol = "123.00"; 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); stringLitersOfPetrol = df.format(litersOfPetrol); System.out.println("liters of petrol before putting in editor : "+stringLitersOfPetrol);
And by the way, when you want to use decimals, forget about the existence of double and float, as others have suggested, and just use the BigDecimal object, this will save you a lot of headache.
source share