This line is your problem:

litersOfPetrol = Float.parseFloat(df.format(litersOfPetrol));

There you formatted your float to string as you wanted, but but then that string got transformed again to a float, and then what you printed in stdout was your float that got a 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 the existence of double and float as others suggested and just use BigDecimal object, it will save you a lot of headache.

Answer from David Hofmann on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java string › convert string to float and back in java
Convert String to Float and Back in Java | Baeldung
January 8, 2024 - Float givenFloat = 1.25f; String result = String.format("%f", givenFloat); assertEquals("1.250000", result); When we format the floating-point number specifying the number of decimal places, the format() method will also round up the result: ...
🌐
Attacomsian
attacomsian.com › blog › java-round-double-float
Round a double or float number to 2 decimal places in Java
November 26, 2022 - The only drawback is there is no way to set the rounding mode. double price = 19.5475; System.out.println("Price: " + String.format("%.2f", price)); // Price: 19.55 · The Math.round() method is another way to round a double or floating point ...
🌐
Spiceworks
community.spiceworks.com › programming & development
Best method to convert string(implied 2 decimal) to doub le? - Programming & Development - Spiceworks Community
December 16, 2003 - I am just learning Java and have a need to take a string value and convert it to a double. At the same time I need to of course add the decimal since the inbound value is 2 decimal place implicit. The below function work…
🌐
Coderanch
coderanch.com › t › 659719 › java › return-double-decimal-points
How to return double with two decimal points (Java in General forum at Coderanch)
Of course, there's a fundamental problem with that. The IEEE floating-point format that Java uses has binary fractions, not decimal ones, so it would be impossible to return a "double with 2 decimal places" for the same reason that you can't return 1/3 to 2 decimal places.
🌐
BeginnersBook
beginnersbook.com › 2022 › 11 › java-string-to-float-conversion
Java String to float Conversion
In case, you want to display float number upto a certain number of decimal places then you can use the %.nf format specifier which tells the compiler to display number upto n decimal places only. Here we want to display only two decimal places so we used %.2f. public class JavaExample{ public ...
Find elsewhere
🌐
How to do in Java
howtodoinjava.com › home › string › convert float to string in java
Convert Float to String in Java - HowToDoInJava
January 9, 2023 - We need to use NumberFormat.format(float) method to format float value to string in predefined format – such as set decimal places in the formatted string. For example, we can format float to 2 decimal points as in the given program.
🌐
iCert Global
icertglobal.com › home › community › how can i format a float to exactly 2 decimal places in java for financial reporting?
How can I format a float to exactly 2 decimal places in Java for financial reporting? | iCert Global
The most straightforward way to handle this for simple console output is using System.out.printf("%.2f", yourFloat);. However, if you need to store the formatted value as a string for a UI or a report, String.format("%.2f", yourFloat) is the ...
🌐
CodingNConcepts
codingnconcepts.com › java › format-number-to-2-decimal-places
Format number to 2 decimal places in Java - Coding N Concepts
December 1, 2022 - Output int: 12345.00 long: 12345.00 float: 12345.60 double: 12345.68 ... By default, DecimalFormat use RoundingMode.HALF_EVEN for rounding. You can use other rounding modes as follows:- import java.math.RoundingMode; import java.text.DecimalFormat; public class FormatAnyNumber { private static final DecimalFormat df = new DecimalFormat("0.00"); public static void main(String[] args) { double num = 12345.6789; df.setRoundingMode(RoundingMode.UP); System.out.println("double rounding up : " + df.format(num)); df.setRoundingMode(RoundingMode.DOWN); System.out.println("double rounding down: " + df.format(num)); } }
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Format-double-Java-printf-example
How to format a Java double with printf example
To use Java printf to format double and float values with decimal place precision, follow these two rules: Use %f as the double specifier in the text string.
🌐
Java67
java67.com › 2014 › 06 › how-to-format-float-or-double-number-java-example.html
5 Examples of Formatting Float or Double Numbers to String in Java | Java67
Coming back to String's format method, here is a quick example of using it : String strDouble = String.format("%.2f", 1.23456); This will format the floating point number 1.23456 up-to 2 decimal places, because we have used two after decimal ...
🌐
LabEx
labex.io › tutorials › java-how-to-convert-a-java-float-to-a-string-413966
How to convert a Java float to a String | LabEx
If you need to control the number of decimal places in the resulting String, you can use the String.format() method with the appropriate format specifier. float myFloat = 3.14159f; String myString = String.format("%.2f", myFloat); ...
🌐
Reddit
reddit.com › r/javahelp › how to convert string to double without losing precision after decimal point?
How to convert string to double without losing precision after decimal point? : r/javahelp
July 21, 2021 - Double is floating point, not fixed point. Use BigDecimal, if you need fixed precision. ... This is correct. Your Double data type can store something like 5.12345 pretty accurately, but it won't be able to store something like 123451234512345.12345. Its precision is defined in terms of the ...