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.
🌐
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") .
🌐
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.
🌐
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[]) ...
🌐
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.
Find elsewhere
🌐
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.
🌐
Scaler
scaler.com › home › topics › convert double to string in java
Convert double to String in Java - Scaler Topics
January 11, 2024 - 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.