This is the second solution to find the length of a given double value. This approach uses Double wrapper class to convert to string before using 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 number is="+doubleNum);
// Use the Wrapper class of an decimal i.e. Double to find out the length
String[] dblToStr=Double.toString(doubleNum).split("\\.");
System.out.println("The Length of the given double number "+doubleNum+" before . is="+dblToStr[0].length());
System.out.println("The Length of the given double number "+doubleNum+" after . is="+dblToStr[1].length());
}
}
Output:
Enter Double number=
3345821.309821
The Double number is=3345821.309821
The Length of the given double number 3345821.309821 before . is=7
The Length of the given double number 3345821.309821 after . is=6

Leave a comment