How to convert float to String in Java
Example:
Input: num=1320.23
Output: "1320.23"
Approach: 1: Using toString()
Java
public class FloatToString {public static void main(String[] args) {float num = 1320.23f;String numFloat = floatToString(num);System.out.println("String is " + numFloat);}private static String floatToString(float num) {return Float.toString(num);}}
Approach : 2: Using valueOf()
Java
public class FloatToString {public static void main(String[] args) {float num = 1320.23f;String numFloat = floatToString(num);System.out.println("String is " + numFloat);}private static String floatToString(float num) {return String.valueOf(num);}}
No comments:
Post a Comment