Using Java’s Float class.

float f = Float.parseFloat("25");
String s = Float.toString(25.0f);

To compare it's always better to convert the string to float and compare as two floats. This is because for one float number there are multiple string representations, which are different when compared as strings (e.g. "25" != "25.0" != "25.00" etc.)

Answer from Petar Ivanov on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java string › convert string to float and back in java
Convert String to Float and Back in Java | Baeldung
January 8, 2024 - First, let’s look at the most common ways to convert Float values to String. The most straightforward solution we can use is concatenating the floating-point value with an empty String.
Discussions

How to convert String to a float? I know for new Float( ) or Float.parseFloat( ) or Float.valueOf( ) tricks, but they don't work.
I need to put a String in sePrice(float price) setter, but then I get this error: Exception in thread "JavaFX Application Thread"… More on reddit.com
🌐 r/javahelp
13
0
January 13, 2020
comma or dot when transform float to String. - Processing 2.x and 3.x Forum
Processing is an electronic sketchbook, a language and a worldwide community. This is its forum. More on forum.processing.org
🌐 forum.processing.org
Need help converting String to float

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions

  • You include any and all error messages in full

  • You ask clear questions

  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

More on reddit.com
🌐 r/javahelp
15
1
August 15, 2021
Ryū: fast float-to-string conversion (Java lib, 2018 paper)

Raffaello Giulietti has improved on this and proposed an implementation to JDK, see e.g. https://mail.openjdk.java.net/pipermail/core-libs-dev/2021-February/073954.html. Sadly, it has not yet been integrated as the change is quite complex and not very many people took the challenge to review it. It would help if somebody (or the author) took the original patch and proposed it again in a GitHub PR as the original comes from pre-Github days.

The results are very impressive, though:

On my laptop hardware, the speedup factor is 17.7x wrt OpenJDK.

More on reddit.com
🌐 r/java
13
56
March 7, 2021
🌐
LabEx
labex.io › tutorials › java-java-float-to-string-conversion-117684
Convert Java Float to String Easily | LabEx
In the terminal, navigate to the project directory by typing the following command: ... Declare two float variables a and b and initialize them with float values of your choice. ... Use the Float.toString() method to convert the float variables ...
🌐
BeginnersBook
beginnersbook.com › 2015 › 05 › java-float-to-string
Java – float to String conversion
September 11, 2022 - We can convert a float to String using any of the following two methods: 1) Method 1: Using String.valueOf(float f): We pass the float value to this method as an argument and it returns the string representation of it.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-convert-a-float-value-to-string
Java Program to Convert a Float value to String - GeeksforGeeks
July 12, 2025 - Below is the implementation of ... Convert float value to String value // using + operator method String stringValue = "" + floatValue; return (stringValue); } // Driver code public static void main(String[] args) { // The ...
🌐
Java67
java67.com › 2015 › 05 › how-to-convert-string-to-float-in-java.html
How to convert String to Float in Java and vice-versa - Tutorial | Java67
You can also see Core Java Volume 1 - Fundamentals by Cay S. Horstmann to learn more about different static factory methods to convert one type of variable to another primitive or wrapper type in Java. BTW, if you already have Float object then you don't need to do String concatenation because calling Float.toString() will anyway give you floating point String you are looking for.
🌐
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 - The Float.toString() method returns a String representation of the float value passed as the argument.
Find elsewhere
🌐
Reddit
reddit.com › r/javahelp › how to convert string to a float? i know for new float( ) or float.parsefloat( ) or float.valueof( ) tricks, but they don't work.
r/javahelp on Reddit: How to convert String to a float? I know for new Float( ) or Float.parseFloat( ) or Float.valueOf( ) tricks, but they don't work.
January 13, 2020 - It looks to me like t.getNewValue() should return a String, in such a case Float.parseFloat(t.getNewValue()) should work as you are passing a primitive. You'll need to handle a NumberFormatException as u/Northeastpaw mentioned. ... I've tried that, but then I get this exception: Thread" java.lang.ClassCastException: class java.lang.Float cannot be cast to class java.lang.String (java.lang.Float and java.lang.String are in module java.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-convert-a-string-value-to-float-value-in-java-with-examples
Convert String to Float in Java - GeeksforGeeks
July 12, 2025 - ... // Java Program to convert string to float import java.util.*; class GFG { // Function to convert String to Float public static float StringToFloat(String s) { // Convert string to float // using valueOf() method return Float.valueOf(s); ...
🌐
Javatpoint
javatpoint.com › java-string-to-float
Java String to float - Javatpoint
Java Convert String to float 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.
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › lang › Float.html
Float (Java SE 17 & JDK 17)
January 20, 2026 - Adds two float values together as per the + operator. ... Returns a hexadecimal string representation of the float argument.
🌐
LabEx
labex.io › tutorials › java-java-float-tostring-method-117686
Java Float toString Method: Convert Float to String | LabEx
Learn how to use the Java Float class's toString() method to convert a float value into a string.
🌐
TutorialsPoint
tutorialspoint.com › convert-from-float-to-string-in-java
Convert from float to String in Java
June 26, 2020 - To convert float to string, use the toString() method. It represents a value in a string. Let’s say the following is our float. float f = 0.9F; Converting the float value to string.
🌐
Java Guides
javaguides.net › 2018 › 08 › java-convert-float-to-string-example.html
Convert Float to String in Java
June 15, 2024 - String.valueOf(floatValue) converts the float value to its string representation. The DecimalFormat class allows you to format decimal numbers to a specific pattern, which can then be converted to a string. import java.text.DecimalFormat; public ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Float.html
Float (Java Platform SE 8 )
3 weeks ago - Java™ Platform Standard Ed. 8 ... The Float class wraps a value of primitive type float in an object. An object of type Float contains a single field whose type is float. In addition, this class provides several methods for converting a float to a String and a String to a float, as well as ...
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › Float.html
Float (Java Platform SE 7 )
Java™ Platform Standard Ed. 7 ... The Float class wraps a value of primitive type float in an object. An object of type Float contains a single field whose type is float. In addition, this class provides several methods for converting a float to a String and a String to a float, as well as ...
🌐
Oracle
docs.oracle.com › en › java › javase › 22 › docs › api › java.base › java › lang › Float.html
Float (Java SE 22 & JDK 22)
July 16, 2024 - Adds two float values together as per the + operator. ... Returns a hexadecimal string representation of the float argument.
🌐
Processing Forum
forum.processing.org › two › discussion › 17682 › comma-or-dot-when-transform-float-to-string.html
comma or dot when transform float to String. - Processing 2.x and 3.x Forum
float f = 1.2 ; String s_1 = Float.toString(f); String s_2 = String.format("%.2f", f) ; println(s_1) ; println(s_2) ; ... import java.util.Locale; import java.text.NumberFormat; float f = 1.2 ; Locale french = new Locale("fr"); NumberFormat frenchNF = NumberFormat.getInstance(french); println("french: "); println(frenchNF.format(f)) ; Locale english = new Locale("en"); NumberFormat englishNF = NumberFormat.getInstance(english); println("english: "); println(englishNF.format(f)) ;
🌐
Studytonight
studytonight.com › java-type-conversion › how-to-convert-java-float-to-string
How to convert Java float to String - Studytonight
Here, a float value is passed in the method and converted into a String formatted up to 2 digits. import java.text.DecimalFormat; public class StudyTonight { public static void main(String args[]) { float n = 500.0878f; Float f = (float)n; String s = new DecimalFormat ("#.00").format (f); //for two places decimal System.out.println("String is : "+s); } }
🌐
IDRSolutions
blog.idrsolutions.com › home › optimising conversion from float to string in java
Optimising conversion from float to String in Java
May 30, 2023 - The method I am going to use is to take my float value, cast it to an integer to chop off any decimal places, then minus the casted value from the original to leave me with only the decimal places. If the result is 0, then I output the int as a String, otherwise I output the float as a String.