Code example for BigDecimal

Methods: scale, unscaledValue

0
    public static Number optimizeNumberRepresentation(Number number)
    { 
        if(number instanceof BigDecimal)
        { 
            BigDecimal bd = (BigDecimal) number;
            if(bd.scale() == 0)
            { 
                // BigDecimal -> BigInteger 
                number = bd.unscaledValue();
            } 
            else 
            { 
                double d = bd.doubleValue();
                if(d != Double.POSITIVE_INFINITY && d != Double.NEGATIVE_INFINITY)
                { 
                    // BigDecimal -> Double 
                    return new Double(d);
                } 
            } 
        } 
        if(number instanceof BigInteger)