it passes the definitely written analysis.
[IRC.git] / Robust / src / Tests / TestDoubleToString.java
1 public class TestDoubleToString {
2   public TestDoubleToString() {
3
4   }
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);
10     t = t/ 10.0d;
11     test = td.alpha(t);
12     System.out.println("t= " + test);
13   }
14
15   //function: converts a number into a string
16   String alpha(double value)
17   { 
18     int i = 0, j = 0, k = 0;
19     long nodecimal = 0;
20     double decimal = 1.0d, valueA = 0.0d;
21     StringBuffer output = new StringBuffer();
22
23     for(i = 0; decimal != nodecimal; i++)
24     {
25       nodecimal = (long) (value*basePower(10, i));
26       decimal = value*basePower(10, i);
27     } //i = place counted from right that decimal point appears
28
29     valueA = nodecimal; //valueA = value with no decimal point (value*10^i)
30
31     for(j = 0; decimal >= 0; j++)
32     {
33       nodecimal = (long) (valueA - basePower(10, j));
34       decimal = (double) nodecimal;
35     } //j-1 = number of digits
36
37     i--;
38     j--;
39     decimal = 0;
40
41     for(k = j; k > 0; k--)
42     {
43       if(k == i) //if a decimal point was previously found
44       {      //insert it where its meant to be
45         //output += (char)46;
46         output.append((char)46);
47       }
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));
52     }
53
54     System.out.println("output= " + output.toString());
55
56     return output.toString();
57   }
58
59   long basePower(int x, int y) {
60     long t = 1;
61     for(int i=0; i<y; i++) {
62       t *= x;
63     }
64     return t;
65   }
66 }