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
🌐
Oracle
docs.oracle.com › javase › tutorial › java › data › converting.html
Converting Between Numbers and Strings (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
The ToStringDemo example uses the toString method to convert a number to a string. The program then uses some string methods to compute the number of digits before and after the decimal point:
🌐
Coderanch
coderanch.com › t › 537690 › java › convert-string-decimal-point-double
convert a string having decimal point to double or long (Java in General forum at Coderanch)
May 12, 2011 - I convert that string to double and then typecast it to int from double??? That is extra effort right?? is there any single method that removes these 2 step and makes it 1?? ... You are confusing some ideas that are really separate. A floating point value of 123.0 can be displayed as 123.0000 on output, it has nothing to do with calling Float.parseFloat().
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-convert-double-to-string
Java Convert double to String | DigitalOcean
August 3, 2022 - We can use Java String format method to convert double to String in our programs. double d = 36.98d; String s = String.format("%f", d); System.out.println(s); //36.980000 · We can use DecimalFormat class to convert double to String.
🌐
Baeldung
baeldung.com › home › java › java numbers › convert double to string, removing decimal places
Convert Double to String, Removing Decimal Places | Baeldung
January 8, 2024 - The NumberFormat class also provides a format method similar to the String class, but NumberFormat is faster and with it, we can specify the rounding mode to achieve either truncation or rounding. The setMaximumFractionDigits() method tells the formatter how many fractional digits after the decimal point to include in the output:
🌐
Rutgers CS
cs.rutgers.edu › courses › 111 › classes › fall_2011_tjang › texts › notes-java › data › strings › conversion › num2string.html
Java: Converting Numbers to Strings
Check the Java API documentation for other options. First, create a DecimalFormat object which specifies the format of the number. The zero before the decimal point means that at least one digit is produced, even if it is zero. The zeros after the decimal point specify how many fraction digits ...
🌐
Scaler
scaler.com › home › topics › convert double to string in java
Convert double to String in Java - Scaler Topics
May 12, 2022 - Java program can be converted from double to string using the String.format() method. It is used to represent double as a string with a specified number of digits after the decimal and rounded values to decimal places.
Find elsewhere
🌐
Coderanch
coderanch.com › t › 377763 › java › Big-Decimal-String
Big Decimal to String (Java in General forum at Coderanch)
June 10, 2024 - Thanks Mei ... To get a string representation of a BigDecimal object, just call the toString() method (inherited from Object). If you need particular formatting, get the doubleValue() and format with java.text.DecimalFormat or use the various Formatter convenience methods if you're using 1.5.
🌐
Javatpoint
javatpoint.com › java-string-to-double
Java Convert String to double
April 1, 2025 - Java Convert String to double example and examples of string to int, int to string, string to date, date to string, string to long, long to string, string to char, char to string, int to long, long to int etc.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-convert-integer-to-string-in-java
Int to String in Java – How to Convert an Integer into a String
September 11, 2022 - You can use it in different ways, but we'll be using it to convert an integer to a string. ... import java.text.DecimalFormat; class IntToStr { public static void main(String[] args) { int age = 2; DecimalFormat DFormat = new DecimalFormat("#"); String AGE_AS_STRING = DFormat.format(age); System.out.println("The child is " + AGE_AS_STRING + " years old"); // The child is 2 years old System.out.println(((Object)age).getClass().getSimpleName()); // Integer System.out.println(AGE_AS_STRING.getClass().getSimpleName()); // String } }
🌐
Online String Tools
onlinestringtools.com › convert-decimal-to-string
Convert Decimal to a String – Online String Tools
Free online decimal to string converter. Just load your decimal and it will automatically get converted to a string. There are no intrusive ads, popups or nonsense, just a decimal to string converter. Load a decimal, get a string.
🌐
Medium
medium.com › @AlexanderObregon › converting-between-strings-and-numbers-in-java-604741d30f4c
Converting Between Strings and Numbers in Java | Medium
June 12, 2025 - Learn how Java converts between strings and numbers using parse methods, value conversion tools, and formatting classes for clean and accurate output.
🌐
CodeGym
codegym.cc › java blog › strings in java › how to convert int to string in java
Convert int to String in Java
January 19, 2026 - convert Integer to String: -7 · java.text.DecimalFormat is a class defined in java.text package and a subclass of NumberFormat. It is used for formatting a decimal number to a string representing following a certain pattern. We can use it for Integers as well.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 8 )
1 week ago - A standard canonical string form of the BigDecimal is created as though by the following steps: first, the absolute value of the unscaled value of the BigDecimal is converted to a string in base ten using the characters '0' through '9' with no leading zeros (except if its value is zero, in which case a single '0' character is used). Next, an adjusted exponent is calculated; this is the negated scale, plus the number of characters in the converted unscaled value, less one. That is, -scale+(ulength-1), where ulength is the length of the absolute value of the unscaled value in decimal digits (its precision).