Float to string in java

Java convert float to string using Float.toString() method

package com.w3schools;

public class FloatToString {
  public static void main(String args[]){
	try {
		float var = -5.24f;
                String str = Float.toString(var);
                System.out.println("String is: "+str);
	} catch (Exception e) {
		e.printStackTrace();
	}
  }
}

Output:

String is: -5.24

Download this example.

Java convert float to string using String.valueOf() method

package com.w3schools;

public class FloatToString {
  public static void main(String args[]){
	try {
		float var = 5.24f;
                String str = String.valueOf(var);
                System.out.println("String is: "+str);
	} catch (Exception e) {
		e.printStackTrace();
	}
  }
}

Output:

String is: 5.24

Download this example.

Please follow and like us:
Content Protection by DMCA.com