1 public class TestDoubleToString {
2 public TestDoubleToString() {
5 public static void main(String args[]) {
6 TestDoubleToString td = new TestDoubleToString();
7 double t = 35.21182666751214;
8 String test = td.alpha(t);
9 System.out.println("t= " + test);
12 System.out.println("t= " + test);
15 //function: converts a number into a string
16 String alpha(double value)
18 int i = 0, j = 0, k = 0;
20 double decimal = 1.0d, valueA = 0.0d;
21 StringBuffer output = new StringBuffer();
23 for(i = 0; decimal != nodecimal; i++)
25 nodecimal = (long) (value*basePower(10, i));
26 decimal = value*basePower(10, i);
27 } //i = place counted from right that decimal point appears
29 valueA = nodecimal; //valueA = value with no decimal point (value*10^i)
31 for(j = 0; decimal >= 0; j++)
33 nodecimal = (long) (valueA - basePower(10, j));
34 decimal = (double) nodecimal;
35 } //j-1 = number of digits
41 for(k = j; k > 0; k--)
43 if(k == i) //if a decimal point was previously found
44 { //insert it where its meant to be
46 output.append((char)46);
48 nodecimal = ((long) (valueA - decimal) / basePower(10, k-1));
49 decimal += nodecimal*basePower(10, k-1);
50 //output += (char)(48 + nodecimal);
51 output.append((char)(48 + nodecimal));
54 System.out.println("output= " + output.toString());
56 return output.toString();
59 long basePower(int x, int y) {
61 for(int i=0; i<y; i++) {