I want to loop through a StringBuffer and use the value of each char to calculate the output. Debug shows that when I pass in 10 that sb.length() is 4 even though it should be 2. Should I be using different methods?
public double ConvertDecimal(double in) {
double out = 0;
sb = new StringBuffer(String.valueOf(Math.floor(in));
sb.reverse();
for (int i = 0; i < sb.length(); i++) {
out += Double.valueOf(sb.charAt(i)) * Math.pow(3,i);
}
return Math.floor(out);
}
For passing in "10" the first iteration makes out=48 and I don't understand where it's going wrong when I want the following:
in = 10
sb.reverse = "01"
//first iteration of loop - char at first position of sb.reverse being 0 out += Double.valueOf(sb.chartAt(0)) * Math.pow(3,0);
//second iteration of loop - char at second position of sb.reverse being 1 out += Double.valueOf(sb.chartAt(1)) * Math.pow(3,1);
return out; //out should be 3