public static double roundMagic(double d) {

        // this function rounds numbers in a good way but slow:
        // - special handling for numbers around 0
        // - only numbers <= +/-1000000000000
        // - convert to a string
        // - check the last 4 characters:
        // '000x' becomes '0000'
        // '999x' becomes '999999' (this is rounded automatically)
        if ((d < 0.0000000000001) && (d > -0.0000000000001)) {
            return 0.0;
        }

        if ((d > 1000000000000.) || (d < -1000000000000.)) {
            return d;
        }

        StringBuffer sb = new StringBuffer();

        sb.append(d);

        int len = sb.length();

        if (len < 16) {
            return d;
        }

        char cx = sb.charAt(len - 1);
        char c1 = sb.charAt(len - 2);
        char c2 = sb.charAt(len - 3);
        char c3 = sb.charAt(len - 4);

        if ((c1 == '0') && (c2 == '0') && (c3 == '0') && (cx != '.')) {
            sb.setCharAt(len - 1, '0');
        } else if ((c1 == '9') && (c2 == '9') && (c3 == '9') && (cx != '.')) {
            sb.setCharAt(len - 1, '9');
            sb.append('9');
            sb.append('9');
        }

        return Double.valueOf(sb.toString()).doubleValue();
    }
public static double roundMagic ( double d ) { if ( ( d < 0.0000000000001 ) && ( d > - 0.0000000000001 ) ) { return 0.0 ; } if ( ( d > 1000000000000. ) || ( d < - 1000000000000. ) ) { return d ; } StringBuffer sb = new StringBuffer ( ) ; sb . append ( d ) ; int len = sb . length ( ) ; if ( len < 16 ) { return d ; } char cx = sb . charAt ( len - 1 ) ; char c1 = sb . charAt ( len - 2 ) ; char c2 = sb . charAt ( len - 3 ) ; char c3 = sb . charAt ( len - 4 ) ; if ( ( c1 == '0' ) && ( c2 == '0' ) && ( c3 == '0' ) && ( cx != '.' ) ) { sb . setCharAt ( len - 1 , '0' ) ; } else if ( ( c1 == '9' ) && ( c2 == '9' ) && ( c3 == '9' ) && ( cx != '.' ) ) { sb . setCharAt ( len - 1 , '9' ) ; sb . append ( '9' ) ; sb . append ( '9' ) ; } return Double . valueOf ( sb . toString ( ) ) . doubleValue ( ) ; }
Retrieves a <em>magically</em> rounded </code>double</code> value produced
from the given <code>double</code> value.  This method provides special
handling for numbers close to zero and performs rounding only for
numbers within a specific range, returning  precisely the given value
if it does not lie in this range. <p>

Special handling includes: <p>

<UL>
<LI> input in the interval -0.0000000000001..0.0000000000001 returns 0.0
<LI> input outside the interval -1000000000000..1000000000000 returns
input unchanged
<LI> input is converted to String form
<LI> input with a <code>String</code> form length greater than 16 returns
input unchaged
<LI> <code>String</code> form with last four characters of '...000x' where
x != '.' is converted to '...0000'
<LI> <code>String</code> form with last four characters of '...9999' is
converted to '...999999'
<LI> the <code>java.lang.Double.doubleValue</code> of the <code>String</code>
form is returned
</UL>
@param d the double value for which to retrieve the <em>magically</em>
rounded value
@return the <em>magically</em> rounded value produced