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 › converting string to bigdecimal in java
Converting String to BigDecimal in Java | Baeldung
January 14, 2026 - In this tutorial, we’ll cover many ways of converting String to BigDecimal in Java. BigDecimal represents an immutable arbitrary-precision signed decimal number.
Discussions

Convert String to double in Java - Stack Overflow
How can I convert a String such as "12.34" to a double in Java? More on stackoverflow.com
🌐 stackoverflow.com
java - How to convert binary string value to decimal - Stack Overflow
How to convert a binary String such as String c = "110010"; // as binary to the value in decimal in Java? (expected result in the example is 50) More on stackoverflow.com
🌐 stackoverflow.com
apex - Function to convert String to Decimal value? - Salesforce Stack Exchange
Stack Exchange network consists ... trusted online community for developers to learn, share their knowledge, and build their careers. Visit Stack Exchange ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I need to convert String into Decimal... More on salesforce.stackexchange.com
🌐 salesforce.stackexchange.com
December 17, 2015
Best method to convert string(implied 2 decimal) to doub le? - Programming & Development - Spiceworks Community
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 works… saying that I know there is probably tons of better ways to do it ... More on community.spiceworks.com
🌐 community.spiceworks.com
0
December 16, 2003
🌐
Online String Tools
onlinestringtools.com › convert-string-to-decimal
Convert a String to Decimal – Online String Tools
Free online string to decimal converter. Just load your string and it will automatically get converted to a decimal string. There are no intrusive ads, popups or nonsense, just a string to decimal converter. Load a string, get its decimal representation.
🌐
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 - Since "Integer.parseInt()" converts a String to an int, where do you think you might find a method which converts a String to a double? And what might you guess the name of the method to be? ... Yes parse double would be fine...But the scenario i had was somethin different which went like this... my method had a system generated sequence. I basically accepted that sequence in sting which meant that the sequence was ending with ".00" .But i had t bring it back to UI in number format and not decimal..( i had to remove that ".00") .
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-convert-binary-string-to-decimal-using-wrapper-class
Java Program to Convert Binary String to Decimal Using Wrapper Class - GeeksforGeeks
May 12, 2022 - // Program to Convert a given Binary String to // its equivalent decimal number import java.lang.Math; import java.util.Scanner; class BinaryToDecimal { // convertToDec() Method // performs the conversion of // Binary to Decimal static int ...
🌐
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)
Here is an example, ValueOfDemo , that gets two strings from the command line, converts them to numbers, and performs arithmetic operations on the values:
🌐
Blogger
javahungry.blogspot.com › 2020 › 01 › string-to-bigdecimal.html
2 ways to Convert String to BigDecimal in Java with Examples | Java Hungry
This is the easiest way to convert String to BigDecimal in java. Just use BigDecimal(String) constructor. BigDecimal obj = new BigDecimal(String); The above line will do the job. You can find the example below: import java.math.*; public class JavaHungry { public static void main(String args[]) ...
Find elsewhere
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › how-to-convert-string-to-double-in-java
Java Convert String to Double examples
September 11, 2022 - There are three ways to convert String to double. 1. Java – Convert String to Double using Double.parseDouble(String) method 2. Convert String to Double in Java using Double.valueOf(String) 3.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-convert-double-to-string
Java Convert double to String | DigitalOcean
August 3, 2022 - double d = 123.454d; String str ... rounded to 2 decimal places System.out.println(str); //str is '123.46' We can use StringBuilder and StringBuffer append function to convert double to string. double d = 123.45d; String str = new StringBuilder().append(d).toString(); Here ...
🌐
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 - Learn to convert float value to String using Float.toString() and String.valueOf() methods and format float to n decimal points. Lokesh Gupta · January 9, 2023 · Java String class · Java String, Number Formatting, String Conversion · Learn ...
🌐
Qlik Community
community.qlik.com › t5 › Design-and-Development › Convert-a-string-to-Big-Decimal › td-p › 2348714
Solved: Convert a string to Big Decimal - Qlik Community - 2348714
November 15, 2024 - The resulting string "6.00" can then be converted to a `BigDecimal` object using its constructor. Note that you may also need to handle any potential exceptions that can be thrown by the `BigDecimal` constructor, such as `NumberFormatException` if the input string is not a valid decimal number. SkyWard Alpine Login ... Ditto - same here! ... You can use the `BigDecimal` class in Java to convert the string "$6.00" to a `BigDecimal` object.
🌐
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…
🌐
Jaspersoft Community
community.jaspersoft.com › forum
Convert the String to Big Decimal with format ("##,##0.00") in Jasperreport - Products - Jaspersoft Community
December 5, 2023 - I would like to convert the field from String to Big Decimal with Format ("##,##0.00") in Java. Previously, we got the DataSource from sql, but for now using csv as datasource. it mean all of the fields is using string format.
🌐
Vultr Docs
docs.vultr.com › java › examples › convert-binary-number-to-decimal-and-vice-versa
Java Program to Convert Binary Number to Decimal and vice-versa | Vultr Docs
September 27, 2024 - Use the Integer.parseInt() method to convert the binary String to its decimal form, specifying the base of the number system (which is 2 for binary). ```java public class BinaryToDecimal { public static void main(String[] args) { String binaryString ...
🌐
Java Guides
javaguides.net › 2019 › 07 › java-conversion-between-bigdecimal-and-string-tutorial.html
How to Convert String into BigDecimal in Java
June 15, 2024 - The BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion. The toString() method provides a canonical representation of a BigDecimal.
🌐
javathinking
javathinking.com › blog › convert-text-to-decimal-java
Converting Text to Decimal in Java — javathinking.com
For applications that require high precision, such as financial calculations, use the BigDecimal class. It provides accurate decimal arithmetic and allows you to control the rounding mode. Before converting a string to a decimal number, validate the input to ensure that it represents a valid decimal number.