This is the second solution to find the length of a given float value. This approach uses Float wrapper class to convert to string before using the length method.
import java.util.Scanner;
public class LengthOfFloatValue {
public static void main(String[] args) {
// TODO Auto-generated method stub
@SuppressWarnings("resource")
Scanner inpFloat= new Scanner(System.in);
//Input an Integer
System.out.println("Enter Float number=");
float floatNum=inpFloat.nextFloat();
System.out.println("The Float value is="+floatNum);
// Use the Wrapper class of an decimal i.e. Float to find out the length
String[] fltToStr=Float.toString(floatNum).split("\\.");
System.out.println("The Length of the given float number "+floatNum+" before . is="+fltToStr[0].length());
System.out.println("The Length of the given float number "+floatNum+" after . is="+fltToStr[1].length());
}
}
Output:
Enter Float number=
34.212
The Float value is=34.212
The Length of the given float number 34.212 before . is=2
The Length of the given float number 34.212 after . is=3

Leave a comment