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.
Float.parseFloat() or Float.valueOf() should work. You'll get a NumberFormatException if the string can't be parsed. From the exception you've posted it looks like you're trying to cast a Float to a String instead of using one of those methods. More on reddit.com
🌐 r/javahelp
13
0
January 13, 2020
converting string to float....
hi, am tryin o read a file and it has datas like as below 6.84464E-03 1.73275E-03 5.26343E-03 1.91723E-02 2.62422E-02 1.72165E-02 5.14687E-03 3.57361E-03 5.69188E-03-5.09780E-03-2.50582E-02-3.34189E... More on forums.oracle.com
🌐 forums.oracle.com
June 11, 2007
Simple and clean java float to string conversion - Stack Overflow
This is a very simple question, but I'm amazed on how difficult it has been to answer. Even the documentation didn't give a clear and straight answer. You see, I'm simply trying to convert a simple More on stackoverflow.com
🌐 stackoverflow.com
[Java] Why does converting a string to float round it?
I would assume that this is not caused by the actual conversion, but by the format that floating point numbers are stored in computers. Floating point numbers are stored in the IEEE 754 format which makes a compromise between precision and storage size. A float in general is shorter and less precise than a double since it uses only half the size (bits - float 32bits vs. double 64bits) to store the information. Whenever possible, you should use either double, but never for monetary values. For monetary values, you should use the BigNumber classes or store the monetary values in the smallest denominator (like in cents) as an integer type (int or long). More on reddit.com
🌐 r/learnprogramming
7
2
December 16, 2016
🌐
Scaler
scaler.com › home › topics › string to float in java
String to Float in Java - Scaler Topics
May 31, 2022 - Strings can be converted to floating point numbers using different methods in Java.
🌐
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 ...
🌐
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 ...
🌐
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); ...
🌐
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.
How to convert String to a float? I know for new ...
January 13, 2020 -

I need to put a String in sePrice(float price) setter, but then I get this error: Exception in thread "JavaFX Application 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.

public class Product {
private SimpleStringProperty name;
private SimpleFloatProperty price;

//I need to to a String here from another method and then convert it to a float.

public void setPrice(float price) {
this.price.set(price);
}
}

I need to use SimpleFloatProperty because I use TableVIew.

Find elsewhere
🌐
Coderanch
coderanch.com › t › 471633 › java › convert-String-float-exact-decimal
How to convert String to float with exact decimal value (Java in General forum at Coderanch)
November 18, 2009 - programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums · this forum made possible by our volunteer staff, including ... ... Hi, When I convert a String value to float by using Float.valueOf(String), The return value is not containing last decimal.
🌐
W3Schools
w3schools.com › java › java_type_casting.asp
Java Type Casting
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... Type casting means converting one data type into another. For example, turning an int into a double. ... Widening Casting (automatic) - converting a smaller type to a larger type size byte -> short -> char -> int -> long -> float -> double
🌐
BeginnersBook
beginnersbook.com › 2022 › 11 › java-string-to-float-conversion
Java String to float Conversion
August 11, 2022 - In such scenario, we can use the valueOf() method of Float class. This method expects the string to contain dot instead of comma so we need to call replace() method of String class to replace the comma with dot. public class JavaExample{ public static void main(String args[]){ String str = ...
🌐
Baeldung
baeldung.com › home › java › java numbers › how to convert double to float in java
How to Convert Double to Float in Java | Baeldung
April 19, 2024 - Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic. In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system: >> Flexible Pub/Sub Messaging With Spring Boot and Dapr · Double and float are data types that represent decimal numbers in Java.
🌐
W3Schools
w3schools.com › java › java_data_types.asp
Java Data Types
Primitive data types - includes byte, short, int, long, float, double, boolean and char · Non-primitive data types - such as String, Arrays and Classes (you will learn more about these in a later chapter) A primitive data type specifies the type of a variable and the kind of values it can hold. There are eight primitive data types in Java: Once a variable is declared with a type, it cannot change to another type later in the program: int myNum = 5; // myNum is an int // myNum = "Hello"; // Error: cannot assign a String to an int String myText = "Hi"; // myText is a String // myText = 123; // Error: cannot assign a number to a String ·
🌐
Oracle
forums.oracle.com › ords › apexds › post › converting-string-to-float-1237
converting string to float....
June 11, 2007 - hi, am tryin o read a file and it has datas like as below 6.84464E-03 1.73275E-03 5.26343E-03 1.91723E-02 2.62422E-02 1.72165E-02 5.14687E-03 3.57361E-03 5.69188E-03-5.09780E-03-2.50582E-02-3.34189E...
🌐
GUVI
guvi.in › hub › java-type-conversion-tutorial › string-to-float
How to Convert String to float in Java?
Our team will reach you out within the next 24 hours.Explore all Programs ... In Java, a String can be converted into a float value by using the Float.parseFloat() method and by using the Float.valueOf() method.
🌐
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); } }