Convert float to String and String to float in Java
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.)
Simple and clean java float to string conversion
System.out.println(String.format("%.1g%n", 0.9425));
System.out.println(String.format("%.1g%n", 0.9525));
System.out.println(String.format( "%.1f", 10.9125));
returns:
0.9
1
10.9
Use the third example for your case
Convert a String to float in java
Your problem is that colon (,) is not a default locale in the JVM...
you can use a NumberFormat for that, with the right locale
String x = "5,828";
NumberFormat myNumForm = NumberFormat.getInstance(Locale.FRENCH);
double myParsedFrenchNumber = (double) myNumForm.parse(x);
System.out.println("D: " + myParsedFrenchNumber);
how does Java convert floats to strings
From the JLS, 4.2.4. Floating-Point Operations:
The Java programming language requires that floating-point arithmetic
behave as if every floating-point operator rounded its floating-point
result to the result precision. Inexact results must be rounded to the
representable value nearest to the infinitely precise result; if the
two nearest representable values are equally near, the one with its
least significant bit zero is chosen. This is the IEEE 754 standard's
default rounding mode known as round to nearest.The Java programming language uses round toward zero when converting a
floating value to an integer (§5.1.3), which acts, in this case, as
though the number were truncated, discarding the mantissa bits.
Rounding toward zero chooses at its result the format's value closest
to and no greater in magnitude than the infinitely precise result.
How to change a float into a String in Java?
Use java Float
class:
String s = Float.toString(25.0f);
if you want to round down a number, simply use the Math.floor()
function.
float f = 2.9999f;
String s = Float.toString(Math.floor(f));//rounds the number to 2 and converts to String
first line rounds the number down to the nearest integer and the second line converts it to a string.
Another way of doing this is using the String.valueOf(floatNumber);
float amount=100.00f;
String strAmount=String.valueOf(amount);
Passing floats to return String in Java
As you are asking for a detailed explanation I will do my best. From what I can understand of your question you have a method methodA(float f1, float f2)
(2 floats as input) and you want to return a String? (Correct me if this is not what you meant).
The "problem" here is that you are using floats (numbers) as input and you want to return text (simply explained, keep in mind that floats are not exact numbers).
Java has a build-in method to do that, called toString
.
Your code would look like this I guess:
public String methodA(float f1, float f2) {
return Float.toString(f1) + " " + Float.toString(f2);
}
To assign the result to the String variable, simply do
public String str;
str = methodA(float f1, float f2)
Now, in short what happened:
- your
methodA
receives two floats as input. It converts the two floats to Strings by theFloat.toString(...)
method and returns them. I added spaces between the two numbers for clarity (" "
, you can delete this or add more) - Create a String variable
str
and call the method on it. The variable now contains the two floats that are converted to a String. - Now do with it what you want.
You said you were a beginner, so I gave a thorough explanation. Hope it helps.
Related Topics
Load a Resource Contained in a Jar
Keylistener Not Working for JPAnel
How to Escape Special HTML Characters in Jsp
Java Unsupported Major Minor Version 52.0
Differencebetween Set and List
Why One Should Prefer Using CSS Over Xpath in Ie
Javafx Tableview Text Alignment
Spring Boot CSS Showing Up Blank/Not Loading After Trying Everything
Resume Http File Download in Java
Android.View.Inflateexception Error Inflating Class Android.Webkit.Webview
How to Get the Subscription Information from Google Play Android Developer API
Android 6.0 Open Failed: Eacces (Permission Denied)
Interview Question: Check If One String Is a Rotation of Other String
What Is More Efficient, I++ or ++I
Java Ee 6 @Javax.Annotation.Managedbean VS. @Javax.Inject.Named VS. @Javax.Faces.Managedbean
How to Save the State of My Program and Then Load It
How to Get a Unique Computer Identifier in Java (Like Disk Id or Motherboard Id)