Convert Float To String Java

Convert Float To String Java | In this blog, let us see how to convert a string to a float in Java. To do this we can use the toString() or valueOf() methods. The method details of the toString() and valueOf() method are as follows:-

  1. public String toString()
  2. public static String toString(float f)
  3. public static String valueOf(float f)
  4. public static String valueOf(Object obj)

See the below examples to understand more:-
1) float= 0.3F
String = 0.3
2) Float= 0.99F
String= 0.99

Converting Float To String In Java using Float.toString()

Every wrapper class contains the toString() method to convert the wrapped & primitive to string type values. In the Float wrapper class the toString() method is given as follows:-

  • public static String toString(float f):- To convert primitive float value to String type.
  • public String toString():- To convert Float wrapped value to String type.

Program to convert primitive float to String value using toString() method

public class Main {
   public static void main(String args[]) {
      float number = 0.99F;
      String string = Float.toString(number);
      System.out.println(string);
   }
}

Output:-

0.99

Program to convert wrapped Float object to String value using toString() method

public class Main {
   public static void main(String args[]) {
      Float number = 1.99F;
      String string = number.toString();
      System.out.println(string);
   }
}

Output:-

1.99

Convert Float To String Java Using String.valueOf()

Let us see how to convert Float to String Java using String.valueOf() method. String class contains valueOf() method with multiple overloaded forms to convert primitive and objects to String type value. The valueOf() method which can be used to convert float/Float to string is given as follows:-

public static String valueOf(float f):- It returns the string representation of the given primitive argument. Internally this method calls Float.toString(f).

How to convert Float to String In Java using the String.valueOf() method?

public class Main {
   public static void main(String[] args){
      float value = 8.36f;
      System.out.println(value);
      String string = String.valueOf(value);
      System.out.println(string);
      System.out.println(string.getClass().getName());
   }
}

Output:-

8.36
8.36
java.lang.String

public static String valueOf(Object obj):- It is used to convert Object type values to the string type. Internally it calls obj.toString() if the given object is not null, else it returns “null”.

public class Main {
   public static void main(String[] args){
      Float value = 9.99f;
      String string = String.valueOf(value);
      System.out.println(string);
   }
}

Output:-

9.99

Also see:- Convert String To Float Java

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *