Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/hartmath/xfunctions/functions/Utilities.java


1   package com.hartmath.xfunctions.functions;
2   
3   public class Utilities {
4   
5      public static String realToString(double x) {
6            // Goal is to get a reasonable representation of x in at most
7            // 10 characters, or 11 characters if x is negative.
8         if (Double.isNaN(x))
9            return "undefined";
10        if (Double.isInfinite(x))
11           if (x < 0)
12              return "-INF";
13           else
14              return "INF";
15        if (Math.abs(x) <= 5000000000.0 && Math.rint(x) == x)
16           return String.valueOf( (long)x );
17        String s = String.valueOf(x);
18        if (s.length() <= 10)
19           return s;
20        boolean neg = false;
21        if (x < 0) {
22           neg = true;
23           x = -x;
24           s = String.valueOf(x);
25        }
26        if (x >= 0.00005 && x <= 50000000 && (s.indexOf('E') == -1 && s.indexOf('e') == -1)) {  // trim x to 10 chars max
27  //         if (s.length() > 10)
28  //             s = s.substring(0,10);
29           s = round(s,10);
30           s = trimZeros(s);
31        }
32        else if (x > 1) { // construct exponential form with positive exponent
33            long power = (long)Math.floor(Math.log(x)/Math.log(10));
34            String exp = "E" + power;
35            int numlength = 10 - exp.length();
36            x = x / Math.pow(10,power);
37            s = String.valueOf(x);
38            s = round(s,numlength);
39  //          if (s.length() > numlength)
40  //             s = s.substring(0,numlength);
41            s = trimZeros(s);
42            s += exp;
43        }
44        else { // constuct exponential form
45            long power = (long)Math.ceil(-Math.log(x)/Math.log(10));
46            String exp = "E-" + power;
47            int numlength = 10 - exp.length();
48            x = x * Math.pow(10,power);
49            s = String.valueOf(x);
50            s = round(s,numlength);
51  //          if (s.length() > numlength)
52  //             s = s.substring(0,numlength);
53            s = trimZeros(s);
54            s += exp;
55        }
56        if (neg)
57           return "-" + s;
58        else
59           return s;
60     }
61     
62     private static String trimZeros(String num) {
63       if (num.indexOf('.') >= 0 && num.charAt(num.length() - 1) == '0') {
64          int i = num.length() - 1;
65          while (num.charAt(i) == '0')
66             i--;
67          if (num.charAt(i) == '.')
68             num = num.substring(0,i);
69          else
70             num = num.substring(0,i+1);
71       }
72       return num;
73     }
74     
75     private static String round(String num, int length) {
76        if (num.indexOf('.') < 0)
77           return num;
78        if (num.length() <= length)
79           return num;
80        if (num.charAt(length) >= '5' && num.charAt(length) != '.') {
81           char[] temp = new char[length+1];
82           int ct = length;
83           boolean rounding = true;
84           for (int i = length-1; i >= 0; i--) {
85              temp[ct] = num.charAt(i); 
86              if (rounding && temp[ct] != '.') {
87                 if (temp[ct] < '9') {
88                    temp[ct]++;
89                    rounding = false;
90                 }
91                 else
92                    temp[ct] = '0';
93              }
94              ct--;
95           }
96           if (rounding) {
97              temp[ct] = '1';
98              ct--;
99           }
100          // ct is -1 or 0
101          return new String(temp,ct+1,length-ct);
102       }
103       else 
104          return num.substring(0,length);
105       
106    }
107    
108 }