This is the first solution to find the length of a given double value. This approach converts double value to string and uses the length method.
import java.util.Scanner;
public class LengthOfDoubleValue {
public static void main(String[] args) {
// TODO Auto-generated method stub
@SuppressWarnings("resource")
Scanner inpDouble= new Scanner(System.in);
//Input an Integer
System.out.println("Enter Double number=");
double doubleNum=inpDouble.nextDouble();
System.out.println("The Double value is="+doubleNum);
// Convert Double number into a String and use the length function
String[] dblToStr=String.valueOf(doubleNum).split("\\.");
System.out.println("The Length of the given float number "+doubleNum+" before . is="+dblToStr[0].length());
System.out.println("The Length of the given float number "+doubleNum+" after . is="+dblToStr[1].length());
}
}
Output:
Enter Double number=
34353.3233
The Double value is=34353.3233
The Length of the given Double number 34353.3233 before . is=5
The Length of the given Double number 34353.3233 after . is=4

Leave a comment