Convert Float to String and String to Float in Java

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:

  1. your methodA receives two floats as input. It converts the two floats to Strings by the Float.toString(...) method and returns them. I added spaces between the two numbers for clarity (" ", you can delete this or add more)
  2. Create a String variable str and call the method on it. The variable now contains the two floats that are converted to a String.
  3. Now do with it what you want.

You said you were a beginner, so I gave a thorough explanation. Hope it helps.



Related Topics



Leave a reply



Submit