This is the first solution to find the length of a given float value. This approach converts float value to string and uses 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);
// Convert long number into a String and use the length function
String[] fltToStr=String.valueOf(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=
342.64
The Float value is=342.64
The Length of the given float number 342.64 before . is=3
The Length of the given float number 342.64 after . is=2

Leave a comment