M
mamta81
Hi all,
I have written a number check function. The purpose is to know number
of digits before and after decimal.
public void checkNumber(double number){
System.out.println("number ------------------->" + number );
Double d = new Double(number);
String n = d.toString();
System.out.println("n" + n);
for(int i =0; i< n.length();i++){
if(n.charAt(i)=='.'){
System.out.println("Is a decimal number");
}
}
int index = n.indexOf(".");
System.out.println("index" + index);
String dec = n.substring(index + 1);
System.out.println("dec " + dec);
if(dec.length() > 6 ){
System.out.println("only 6 digits allowed after decimal");
}
String num = n.substring(0,n.indexOf("."));
System.out.println("num " + num);
if(num.length()>10){
System.out.println("Only 10 places allowed before decimal");
}
}
}
when i give checkNumber( 33333335.2534566d); as input i get the
following o/p
number ------------------->3.33333352534566E7
n3.33333352534566E7
Is a decimal number
index1
dec 33333352534566E7
only 6 digits allowed after decimal
num 3
1) what happens to my input for which I get a wrong index of ". "?
2) Is there any other way to find the number of digits before and
after decimal?
I have written a number check function. The purpose is to know number
of digits before and after decimal.
public void checkNumber(double number){
System.out.println("number ------------------->" + number );
Double d = new Double(number);
String n = d.toString();
System.out.println("n" + n);
for(int i =0; i< n.length();i++){
if(n.charAt(i)=='.'){
System.out.println("Is a decimal number");
}
}
int index = n.indexOf(".");
System.out.println("index" + index);
String dec = n.substring(index + 1);
System.out.println("dec " + dec);
if(dec.length() > 6 ){
System.out.println("only 6 digits allowed after decimal");
}
String num = n.substring(0,n.indexOf("."));
System.out.println("num " + num);
if(num.length()>10){
System.out.println("Only 10 places allowed before decimal");
}
}
}
when i give checkNumber( 33333335.2534566d); as input i get the
following o/p
number ------------------->3.33333352534566E7
n3.33333352534566E7
Is a decimal number
index1
dec 33333352534566E7
only 6 digits allowed after decimal
num 3
1) what happens to my input for which I get a wrong index of ". "?
2) Is there any other way to find the number of digits before and
after decimal?