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
May 19, 2021 - We can convert a String into BigDecimal in Java using one of the below methods: ... Let’s discuss each of them below. The easiest way to convert String to BigDecimal in Java is to use BigDecimal(String) constructor:
🌐
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)
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().
🌐
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 ...
🌐
Javatpoint
javatpoint.com › java-string-to-double
Java String to double
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.
🌐
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 ...
🌐
CodeProject
codeproject.com › Questions › 1008182 › Converting-String-containing-large-decimal-numbers
[Solved] Converting String containing large decimal numbers to decimal number using java - CodeProject
JavaScript · Internet of Things · C / C++ / MFC> ATL / WTL / STL · Managed C++/CLI · C# Free Tools · Objective-C and Swift · Database · Hardware & Devices> System Admin · Hosting and Servers · Java · Linux Programming · Python · .NET (Core and Framework) Android ·
Find elsewhere
🌐
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 works… saying that I know there is probably tons of better ways to do it ...
🌐
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 - We then create a `BigDecimal` object `bd` by passing the string `str` to the constructor of `BigDecimal`. However, since the string `str` contains a currency symbol "$" that is not valid in a `BigDecimal` object, we remove it using the `replace()` method. The resulting string "6.00" can then be converted to a `BigDecimal` object using its constructor.
🌐
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 - The long answer is that 5.0 and 5.00000 are the same thing. Internally double has more precision, but when you print a double it only prints with the amount of precision you need. The answer is to print the decimal with a format string as shown in the second line below.
🌐
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.
🌐
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.
🌐
Oracle
docs.oracle.com › middleware › 1213 › wls › WLAPI › weblogic › wtc › jatmi › Decimal.html
Decimal (Java API Reference for Oracle WebLogic Server)
Constructs a newly allocated Decimal object from a String value. The String is converted to a Decimal value. The String must be in the format accepted by the Java Double.parseDouble method.
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › how-to-convert-string-to-double-in-java
Java Convert String to Double examples
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.
🌐
GeeksforGeeks
geeksforgeeks.org › java › binary-to-decimal-conversion-in-java
Binary to Decimal Conversion in Java - GeeksforGeeks
So the basic idea for converting a binary number to its decimal equivalent is to multiply each digit in the binary number by 2 raised to the power of its positional value and then add up these values.
Published   July 4, 2025