I need to create a program that converts a string to double.
public class Convert{
public static void main(String args[]){
String num="124023211.123";
int d=0,g=0,c=0,fnl=0,h=0,v=0;
double fnl2=0;
int exp=(num.indexOf(".")-1);
while(num.charAt(d)!='.'){
g=num.charAt(d)-48;
int k = 1;
for(int f=0;f<exp;f++){
k=(k*10);
}
fnl+=(k*g);
d++;
exp--;
}
num=(num.substring(d+1) );
//System.out.println(fnl);
//System.out.println(num);
while(h!=num.length()){
v=num.charAt(h)-48;
double j=1;
int exp1=num.length();
for(int f1=0;f1<exp1;f1++){
j*=.10;
}
fnl2+=(h*j);
j++;
h++;
}
System.out.println(fnl2);
}
}
The first while loop converts the int part of the string and it works right. But the second while loop should result to the decimal portion of the string. I am having a hard time because double results to huge decimal numbers and it ruins the conversion, and also the second while loop prints the wrong answer.