How to convert float to String in java


In this core java tutorial we will learn how to convert float primitive type to String in java and convert float object to String with program and examples in java. We will  also understand  When to use convert String to float primitive type and when when to convert String to float object in java.



How to convert String to int or Integer in java



Programs to convert float to String in java -
  • Program/Example 1 to How to convert float primitive type to String  in java.
  • Program/Example 2 to How to convert float object to String in java


Program/Example 1 to How to convert float primitive type to String  in java.

String.valueOf(float) method converts float primitive type to String  in java.

package convertFloatToStringPkg;
public class ConvertFloatToStringExample1 {
   public static void main(String[] args) {
         
          float f = 12f;
          String string = String.valueOf(f);
          System.out.println("string = "+string);
  
   }
}
/*OUTPUT
string = 12.0

*/




Program/Example 2 to How to convert float object to String in java

toString() method of java.lang.Float class convert float object to String in java.

package convertFloatToStringPkg;
public class ConvertFloatObjectToStringExample2 {
   public static void main(String[] args) {
          Float floatObject = 12f;
          String string = floatObject.toString();
          System.out.println("string = "+string);
  
   }
}
/*OUTPUT
string = 12.0
*/



Summary -

In this core java tutorial we learned conversion of float primitive type to String in java and convert float object to String with program and examples in java. We also understood  when to use convert String to float primitive type and when when to convert String to float object in java.




Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy(JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>

How to convert String to float and float to string in java

How to convert String to double and double to string in java


No comments:

Post a Comment