Back to index

wims  3.65+svn20090927
Util.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007-2008 Mihai Preda.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 package org.javia.arity;
00018 
00022 public class Util {
00023     /*
00024     private static String replace(String str, String what, String repl) {
00025         int pos = str.indexOf(what);
00026         while (pos != -1) {
00027             str = str.substring(0, pos) + repl + str.substring(pos + what.length());
00028             pos = str.indexOf(what);
00029         }
00030         //Log.log("replaced: '"+str+"'");
00031         return str;
00032     }
00033     */
00034 
00041     public static double shortApprox(double value, double maxError) {
00042         final double v = Math.abs(value);
00043         final double tail = MoreMath.intExp10(MoreMath.intLog10(Math.abs(maxError)));
00044         final double ret = Math.floor(v/tail +.5)*tail;
00045         return (value < 0) ? -ret : ret;
00046     }
00047 
00054     public static String sizeTruncate(String str, int maxLen) {
00055         int ePos = str.lastIndexOf('E');
00056         String tail = (ePos != -1) ? str.substring(ePos) : "";
00057         int tailLen = tail.length();
00058         int maxHeadLen = maxLen - tailLen;
00059         return str.substring(0, Math.min(str.length()-tailLen, maxHeadLen)) + tail;
00060     }
00061 
00070     public static String doubleToString(double v, int roundingDigits) {        
00071         if (roundingDigits > 13) {
00072             roundingDigits = 0;
00073         }
00074         int roundingStart = roundingDigits == 0 ? 17 : 15 - roundingDigits;
00075 
00076         String str = Double.toString(Math.abs(v));
00077         StringBuffer buf = new StringBuffer(str);
00078         int ePos = str.lastIndexOf('E');
00079         int exp  =  (ePos != -1) ? Integer.parseInt(str.substring(ePos + 1)) : 0;
00080         if (ePos != -1) {
00081             buf.setLength(ePos);
00082         }
00083         int len = buf.length();
00084 
00085         //remove dot
00086         int dotPos;
00087         for (dotPos = 0; dotPos < len && buf.charAt(dotPos) != '.';) ++dotPos;
00088         exp += dotPos;
00089         if (dotPos < len) {
00090             buf.deleteCharAt(dotPos);
00091             --len;
00092         }
00093 
00094         //round
00095         for (int p = 0; p < len && buf.charAt(p) == '0'; ++p) { 
00096             ++roundingStart; 
00097         }
00098 
00099         if (roundingStart < len) {
00100             if (buf.charAt(roundingStart) >= '5') {
00101                 int p;
00102                 for (p = roundingStart-1; p >= 0 && buf.charAt(p)=='9'; --p) {
00103                     buf.setCharAt(p, '0');
00104                 }
00105                 if (p >= 0) {
00106                     buf.setCharAt(p, (char)(buf.charAt(p)+1));
00107                 } else {
00108                     buf.insert(0, '1');
00109                     ++roundingStart;
00110                     ++exp;
00111                 }
00112             }
00113             buf.setLength(roundingStart);
00114         }
00115 
00116         //re-insert dot
00117         if ((exp < -5) || (exp > 10)) {
00118             buf.insert(1, '.');
00119             --exp;
00120         } else {
00121             for (int i = len; i < exp; ++i) {
00122                 buf.append('0');
00123             }
00124             buf.insert((exp<0)? 0 : exp, '.');
00125             for (int i = exp; i <= 0; ++i) {
00126                 buf.insert(0, '0');
00127             }
00128             exp = 0;
00129         }
00130         len = buf.length();
00131         
00132         //remove trailing dot and 0s.
00133         int tail;
00134         for (tail = len-1; tail >= 0 && buf.charAt(tail) == '0'; --tail) {
00135             buf.deleteCharAt(tail);
00136         }
00137         if (tail >= 0 && buf.charAt(tail) == '.') {
00138             buf.deleteCharAt(tail);
00139         }
00140 
00141         if (exp != 0) {
00142             buf.append('E').append(exp);
00143         }
00144         if (v < 0) {
00145             buf.insert(0, '-');
00146         }
00147         return buf.toString();
00148     }
00149 }