KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Made > CDL > Fixed


1 /* $Id: Fixed.java,v 1.1.1.1 2003/02/11 16:19:40 bures Exp $ */
2 package SOFA.SOFAnode.Made.CDL;
3
4 class Fixed {
5   public long number;
6   public int total;
7   public int fract;
8
9   static long pow10(int n) {
10     int i;
11     long ret = 1;
12     for(i=0;i<n;i++)
13       ret*=10L;
14     return ret;
15   }
16
17   public Fixed(String JavaDoc str) {
18     int i;
19     number = total = fract = 0;
20     i = 0;
21     while (str.charAt(i)=='0') {
22       i++;
23     }
24     boolean point = false;
25     int pom = 0, pomfr = 0;
26     while (str.charAt(i)!='d' && str.charAt(i)!='D') {
27       if (str.charAt(i)=='0') {
28         pom++;
29     if (point)
30       /*fract++;*/
31       pomfr++;
32     else {
33       fract--;
34     }
35       } else if (str.charAt(i)=='.') {
36         point = true;
37     fract = 0;
38       } else {
39         if (point) {
40       fract +=pomfr + 1;
41       pomfr = 0;
42     } else {
43       fract = 0;
44     }
45         total += pom+1;
46     number *= pow10(pom+1);
47     number += (double)(str.charAt(i) - '0');
48     pom=0;
49       }
50       i++;
51     }
52   }
53
54   public double toDouble() {
55     if (fract<0)
56       return (double)number * (double)pow10(-fract);
57     return (double)number / (double)pow10(fract);
58   }
59 }
60
Popular Tags