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

Java: transform decimal number to String - Stack Overflow
Then, I cant' use DecimalFormat. There isn't a method to get exactly value as string? – Samuel Benares Commented Sep 26, 2013 at 17:23 · It's unclear how you are reading the String part. Can you update your question to show this? ... I don't know what type is x. May be a int, or a string, ... 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
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
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
🌐
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") .
🌐
CopyProgramming
copyprogramming.com › howto › how-to-convert-string-to-decimal-in-java
Java: Converting a string into a decimal in Java: A Step-by-Step Guide
April 22, 2023 - Java - How to convert char to decimal using an ascii, The problem is you are printing the a b c and d as chars so it will print what ever decimal value of a b c and d correspond to in the ascii table. If you … · Read other technology post: Csharp cast dynamic object to class c ... Converting Java double to byte[] and byte[] to double (using IEEE 754 double-precision binary floating-point format) Appending two bytes to an integer: A step-by-step guide · Converting Strings to Byte Arrays in Java Made Simple
🌐
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.
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › how-to-convert-string-to-double-in-java
Java Convert String to Double examples
September 11, 2022 - public class JavaExample{ public ... System.out.println(dnum); } } ... The valueOf() method of Double wrapper class in Java, works similar to the parseDouble() method that we have seen in the above java example....
Find elsewhere
🌐
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)
int i; double d; String s3 = Integer.toString(i); String s4 = Double.toString(d); 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:
🌐
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 - After converting a string to a BigDecimal, you might want to set a specific scale (number of decimal places) and rounding mode. The BigDecimal.setScale() method allows you to set the scale and rounding mode. import java.math.BigDecimal; import java.math.RoundingMode; public class StringToBigDecimalWithScale { public static void main(String[] args) { String strValue = "12345.6789"; // Convert string to BigDecimal BigDecimal bigDecimalValue = new BigDecimal(strValue); // Set scale and rounding mode BigDecimal scaledValue = bigDecimalValue.setScale(2, RoundingMode.HALF_UP); System.out.println("Original BigDecimal value: " + bigDecimalValue); System.out.println("Scaled BigDecimal value: " + scaledValue); } } Original BigDecimal value: 12345.6789 Scaled BigDecimal value: 12345.68 ·
🌐
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 - Binary String converted to primitive ... number import java.lang.Math; import java.util.Scanner; class BinaryToDecimal { // convertToDec() Method // performs the conversion of // Binary to Decimal static int convertToDec(String bin) ...
🌐
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…
🌐
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 - That second line says print a formatted string, % means print a variable, .5 means print 5 decimal places, f means the variable is floating point, and number is just the variable that stores your number. ... Don't use Double. 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 number of significant digits that it can store, not the number of digits after the decimal point.
🌐
Online String Tools
onlinestringtools.com › convert-string-to-decimal
Convert a String to Decimal – Online String Tools
Instant Copy-to-clipboardCopy the string to clipboard with a single click. ... This tool converts a ASCII, Unicode or UTF8 string to its decimal representation.