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 - String givenString = "1.25"; Float result = new Float(givenString); assertEquals(1.25f, result); As of Java 9, this constructor has been deprecated. Instead, we should consider using the other static factory methods such as parseFloat() or ...
Discussions

Converting string into float from 2d array
If the array contains bare floats, you can convert it to a float array with the float(). This would require splitting and parsing each line individually: ... You have to parse them individually... Something like this in setup(): ... If that doesn't work, then get back to me... I haven't tested it and I don't have the CSV file that you're using. ... So you can iterate a 2D array in 1D in Java... More on forum.processing.org
🌐 forum.processing.org
October 20, 2012
How Do I Convert String To Float?
I had to answer this, because I could find no answer anywhere. I tried Parse and TryParse. I wrote my string to the Debug Log and saw that the string contained only a number. So, I tried the Try Catch and found the following error: · Here is my solution: OKtoFire = true; string angle1 = ... More on discussions.unity.com
🌐 discussions.unity.com
1
0
August 16, 2022
Converting String into Float
Hi all, I wanna ask you about converting string into float. I used method parseFloat() to do it and… More on forum.katalon.com
🌐 forum.katalon.com
0
May 27, 2018
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
🌐
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.

🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-convert-string-to-float-value
Java Program to Convert String to Float Value - GeeksforGeeks
August 5, 2021 - ... // Java Program to Convert String to Float Value by // Implementing parseFloat() method of Float class // Importing input output classes import java.io.*; // Main class class GFG { // Main driver method public static void main(String[] args) ...
🌐
Processing Forum
forum.processing.org › topic › converting-string-into-float-from-2d-array
Converting string into float from 2d array
October 20, 2012 - or is this default to outputValues[i][0] = float(csv[i][0]); ?? float[][] outputValues = new float[data.length][csvWidth]; ... In Java, a two-dimensional array is just an array of arrays; for example, the element csv[2] is the third element (int this case array) in that array.
🌐
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); ...
🌐
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.
Find elsewhere
🌐
Unity
discussions.unity.com › unity engine
How Do I Convert String To Float? - Unity Engine - Unity Discussions
August 16, 2022 - I wrote my string to the Debug Log and saw that the string contained only a number. So, I tried the Try Catch and found the following error: · Here is my solution: OKtoFire = true; string angle1 = ...
🌐
Scaler
scaler.com › home › topics › string to float in java
String to Float in Java - Scaler Topics
May 31, 2022 - The parseFloat() method is a static method of Float class used to convert string to float in java. It takes the string to be parsed as its input and returns the float type of the string.
🌐
Katalon
forum.katalon.com › archive
Converting String into Float - Archive - Katalon Community
May 27, 2018 - Hi all, I wanna ask you about converting string into float. I used method parseFloat() to do it and…
🌐
Java Code Geeks
javacodegeeks.com › home › jvm languages › kotlin
How To Convert String to Float in Kotlin? - Java Code Geeks
January 11, 2021 - Converting string to float is done with toFloat() method of string class. toFloat() method is to parse the String value into Float. If the string is not a valid form of a number or any non-number presents then it throws NumberFormatException. ...
🌐
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
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 = ...
🌐
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.
🌐
Coderanch
coderanch.com › t › 388967 › java › String-Float
String to Float (Beginning Java forum at Coderanch)
June 18, 2001 - String value = "0.999" ; Float floatObject = new Float( value ) ; System.out.println( floatObject.floatValue() ) ; [This message has been edited by Richard Boren (edited June 18, 2001).] ... Is this what you meant Richard? This might be better (doubles are better): Doubles are usually the better ...
🌐
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...
🌐
Reddit
reddit.com › r/learnprogramming › [java] why does converting a string to float round it?
r/learnprogramming on Reddit: [Java] Why does converting a string to float round it?
December 16, 2016 -
	System.out.println("Starting");
	String startnum = "25+422.159";
	System.out.println("startnum = " + startnum);
	String formatnum = startnum.replace("+", "");
	System.out.println("formatnum = " + formatnum);
	float floatnum = Float.parseFloat(formatnum);
	System.out.println("floatnum = " + floatnum);
	System.out.println("Ending");    

Gives me the following output

Starting
startnum = 25+422.159
formatnum = 25422.159
floatnum = 25422.158
Ending

So for some reason, the float value gets rounded down. And for some reason, when I passed this value to another method to put into a database, the value was 25422.158203125

So how do I stop this weird ness

🌐
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 - ... Deprecated, for removal: This API element is subject to removal in a future version. It is rarely appropriate to use this constructor. Use parseFloat(String) to convert a string to a float primitive, or use valueOf(String) to convert a string to a Float object.