Back to index

wims  3.65+svn20090927
Static Public Member Functions
org.javia.arity.Util Class Reference

Contains static helper methods for formatting double values. More...

List of all members.

Static Public Member Functions

static double shortApprox (double value, double maxError)
 Returns a number which is an approximation of v (within maxError) and which has fewer digits in base-10).
static String sizeTruncate (String str, int maxLen)
 Returns an approximation with no more than maxLen chars.
static String doubleToString (double v, int roundingDigits)
 Rounds by dropping roundingDigits of double precision (similar to 'hidden precision digits' on calculators), and formats to String.
static double shortApprox (double value, double maxError)
 Returns a number which is an approximation of v (within maxError) and which has fewer digits in base-10).
static String sizeTruncate (String str, int maxLen)
 Returns an approximation with no more than maxLen chars.
static String doubleToString (double v, int roundingDigits)
 Rounds by dropping roundingDigits of double precision (similar to 'hidden precision digits' on calculators), and formats to String.

Detailed Description

Contains static helper methods for formatting double values.

Definition at line 22 of file Util.java.


Member Function Documentation

static String org.javia.arity.Util.doubleToString ( double  v,
int  roundingDigits 
) [inline, static]

Rounds by dropping roundingDigits of double precision (similar to 'hidden precision digits' on calculators), and formats to String.

Parameters:
vthe value to be converted to String
roundingDigitsthe number of 'hidden precision' digits (e.g. 2).
Returns:
a String representation of v

Definition at line 70 of file Util.java.

                                                                      {        
        if (roundingDigits > 13) {
            roundingDigits = 0;
        }
        int roundingStart = roundingDigits == 0 ? 17 : 15 - roundingDigits;

        String str = Double.toString(Math.abs(v));
        StringBuffer buf = new StringBuffer(str);
        int ePos = str.lastIndexOf('E');
        int exp  =  (ePos != -1) ? Integer.parseInt(str.substring(ePos + 1)) : 0;
        if (ePos != -1) {
            buf.setLength(ePos);
        }
        int len = buf.length();

        //remove dot
        int dotPos;
        for (dotPos = 0; dotPos < len && buf.charAt(dotPos) != '.';) ++dotPos;
        exp += dotPos;
        if (dotPos < len) {
            buf.deleteCharAt(dotPos);
            --len;
        }

        //round
        for (int p = 0; p < len && buf.charAt(p) == '0'; ++p) { 
            ++roundingStart; 
        }

        if (roundingStart < len) {
            if (buf.charAt(roundingStart) >= '5') {
                int p;
                for (p = roundingStart-1; p >= 0 && buf.charAt(p)=='9'; --p) {
                    buf.setCharAt(p, '0');
                }
                if (p >= 0) {
                    buf.setCharAt(p, (char)(buf.charAt(p)+1));
                } else {
                    buf.insert(0, '1');
                    ++roundingStart;
                    ++exp;
                }
            }
            buf.setLength(roundingStart);
        }

        //re-insert dot
        if ((exp < -5) || (exp > 10)) {
            buf.insert(1, '.');
            --exp;
        } else {
            for (int i = len; i < exp; ++i) {
                buf.append('0');
            }
            buf.insert((exp<0)? 0 : exp, '.');
            for (int i = exp; i <= 0; ++i) {
                buf.insert(0, '0');
            }
            exp = 0;
        }
        len = buf.length();
        
        //remove trailing dot and 0s.
        int tail;
        for (tail = len-1; tail >= 0 && buf.charAt(tail) == '0'; --tail) {
            buf.deleteCharAt(tail);
        }
        if (tail >= 0 && buf.charAt(tail) == '.') {
            buf.deleteCharAt(tail);
        }

        if (exp != 0) {
            buf.append('E').append(exp);
        }
        if (v < 0) {
            buf.insert(0, '-');
        }
        return buf.toString();
    }

Here is the caller graph for this function:

static String org.javia.arity.Util.doubleToString ( double  v,
int  roundingDigits 
) [inline, static]

Rounds by dropping roundingDigits of double precision (similar to 'hidden precision digits' on calculators), and formats to String.

Parameters:
vthe value to be converted to String
roundingDigitsthe number of 'hidden precision' digits (e.g. 2).
Returns:
a String representation of v

Definition at line 70 of file Util.java.

                                                                      {        
        if (roundingDigits > 13) {
            roundingDigits = 0;
        }
        int roundingStart = roundingDigits == 0 ? 17 : 15 - roundingDigits;

        String str = Double.toString(Math.abs(v));
        StringBuffer buf = new StringBuffer(str);
        int ePos = str.lastIndexOf('E');
        int exp  =  (ePos != -1) ? Integer.parseInt(str.substring(ePos + 1)) : 0;
        if (ePos != -1) {
            buf.setLength(ePos);
        }
        int len = buf.length();

        //remove dot
        int dotPos;
        for (dotPos = 0; dotPos < len && buf.charAt(dotPos) != '.';) ++dotPos;
        exp += dotPos;
        if (dotPos < len) {
            buf.deleteCharAt(dotPos);
            --len;
        }

        //round
        for (int p = 0; p < len && buf.charAt(p) == '0'; ++p) { 
            ++roundingStart; 
        }

        if (roundingStart < len) {
            if (buf.charAt(roundingStart) >= '5') {
                int p;
                for (p = roundingStart-1; p >= 0 && buf.charAt(p)=='9'; --p) {
                    buf.setCharAt(p, '0');
                }
                if (p >= 0) {
                    buf.setCharAt(p, (char)(buf.charAt(p)+1));
                } else {
                    buf.insert(0, '1');
                    ++roundingStart;
                    ++exp;
                }
            }
            buf.setLength(roundingStart);
        }

        //re-insert dot
        if ((exp < -5) || (exp > 10)) {
            buf.insert(1, '.');
            --exp;
        } else {
            for (int i = len; i < exp; ++i) {
                buf.append('0');
            }
            buf.insert((exp<0)? 0 : exp, '.');
            for (int i = exp; i <= 0; ++i) {
                buf.insert(0, '0');
            }
            exp = 0;
        }
        len = buf.length();
        
        //remove trailing dot and 0s.
        int tail;
        for (tail = len-1; tail >= 0 && buf.charAt(tail) == '0'; --tail) {
            buf.deleteCharAt(tail);
        }
        if (tail >= 0 && buf.charAt(tail) == '.') {
            buf.deleteCharAt(tail);
        }

        if (exp != 0) {
            buf.append('E').append(exp);
        }
        if (v < 0) {
            buf.insert(0, '-');
        }
        return buf.toString();
    }
static double org.javia.arity.Util.shortApprox ( double  value,
double  maxError 
) [inline, static]

Returns a number which is an approximation of v (within maxError) and which has fewer digits in base-10).

Parameters:
valuethe value to be approximated
maxErrorthe maximum deviation from value
Returns:
an approximation with a more compact base-10 representation.

Definition at line 41 of file Util.java.

                                                                    {
        final double v = Math.abs(value);
        final double tail = MoreMath.intExp10(MoreMath.intLog10(Math.abs(maxError)));
        final double ret = Math.floor(v/tail +.5)*tail;
        return (value < 0) ? -ret : ret;
    }

Here is the call graph for this function:

static double org.javia.arity.Util.shortApprox ( double  value,
double  maxError 
) [inline, static]

Returns a number which is an approximation of v (within maxError) and which has fewer digits in base-10).

Parameters:
valuethe value to be approximated
maxErrorthe maximum deviation from value
Returns:
an approximation with a more compact base-10 representation.

Definition at line 41 of file Util.java.

                                                                    {
        final double v = Math.abs(value);
        final double tail = MoreMath.intExp10(MoreMath.intLog10(Math.abs(maxError)));
        final double ret = Math.floor(v/tail +.5)*tail;
        return (value < 0) ? -ret : ret;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

static String org.javia.arity.Util.sizeTruncate ( String  str,
int  maxLen 
) [inline, static]

Returns an approximation with no more than maxLen chars.

Parameters:
strthe value to truncate (e.g. "-2.898983455E20")
maxLenthe maximum number of characters in the returned string
Returns:
a truncation no longer then maxLen (e.g. "-2.8E20" for maxLen=7).

Definition at line 54 of file Util.java.

                                                              {
        int ePos = str.lastIndexOf('E');
        String tail = (ePos != -1) ? str.substring(ePos) : "";
        int tailLen = tail.length();
        int maxHeadLen = maxLen - tailLen;
        return str.substring(0, Math.min(str.length()-tailLen, maxHeadLen)) + tail;
    }
static String org.javia.arity.Util.sizeTruncate ( String  str,
int  maxLen 
) [inline, static]

Returns an approximation with no more than maxLen chars.

Parameters:
strthe value to truncate (e.g. "-2.898983455E20")
maxLenthe maximum number of characters in the returned string
Returns:
a truncation no longer then maxLen (e.g. "-2.8E20" for maxLen=7).

Definition at line 54 of file Util.java.

                                                              {
        int ePos = str.lastIndexOf('E');
        String tail = (ePos != -1) ? str.substring(ePos) : "";
        int tailLen = tail.length();
        int maxHeadLen = maxLen - tailLen;
        return str.substring(0, Math.min(str.length()-tailLen, maxHeadLen)) + tail;
    }

The documentation for this class was generated from the following files: