Convert float to String in Java

We can convert float to String in Java using the following methods:

  • String.valueOf() method
  • Float.toString() method

Convert float to String in Java using the String.valueOf() method

There are multiple overloaded versions of the valueOf() method from the String class. We will use the one which accepts float.

Example

class Test {

  public static void main(String[] args) {

    float f = 123.5f;

    String str = String.valueOf(f);

    System.out.println(str);
  }
}
Output: 123.5

Parse float to String using the Float.toString() method

We can also use the static method toString() of the Float class. It returns a String representation of a provided float.

Example

class Test {

  public static void main(String[] args) {

    float f = 123.5f;

    String str = Float.toString(f);

    System.out.println(str);
  }
}
Output: 123.5
 
That was all about how to convert float to String in Java. Proceed to the next lesson.
 
Happy coding!

Leave a Reply

Your email address will not be published.