Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Below is the code I’ve which I have to make an assertion between 2 values String(which I got from table) and float value: 

String valueFromTable = "25";

Float valueCalculated =25.0;

Code for float to string:

String sSelectivityRate = String.valueOf(valueCalculated );

Here the assertion fails. Can anyone tell me how to convert float to string and string to float?

1 Answer

0 votes
by (19.7k points)

You can use Java’s Float class like below: 

float f = Float.parseFloat("25");

String s = Float.toString(25.0f);

The better way is to convert the string to float and compare as two floats. Because a float can have multiple string representations, which turns out to be different if you compare it as strings(e.g. "25" != "25.0" != "25.00" etc.)

Interested in Java? Check out this Java tutorial by Intellipaat. 

Related questions

0 votes
1 answer
asked Apr 11, 2021 in Java by sheela_singh (9.5k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...