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