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.)

Leave a Comment