public static decimal ToDecimal(JavaNumber n)
            {
                if (n is JavaByte || n is JavaShort || n is JavaInteger)
                {
                    return new decimal(n.intValue());
                }
                else if (n is JavaLong)
                {
                    return new decimal(n.longValue());
                }
                else if (n is JavaFloat
                         || n is JavaDouble)
                {
                    return new decimal(n.doubleValue());
                }

                JavaBigDecimal bigDecimalValue = n as JavaBigDecimal;

                if (bigDecimalValue != null)
                {
                    if (bigDecimalValue.compareTo(MAX_DECIMAL) > 0
                        || bigDecimalValue.compareTo(MIN_DECIMAL) < 0)
                    {
                        throw HsqlConvert.NumericValueOutOfRange(n);
                    }

                    return decimal.Parse(bigDecimalValue.toPlainString());
                }

                return decimal.Parse(n.toString());
            }
            public static bool ToBoolean(JavaNumber n)
            {
                if (n is JavaInteger
                    || n is JavaShort
                    || n is JavaByte)
                {
                    return (n.intValue() != 0);
                }
                else if (n is JavaLong)
                {
                    return (n.longValue() != 0L);
                }
                else if (n is JavaDouble
                         || n is JavaFloat)
                {
                    return (n.doubleValue() != 0.0D);
                }

                JavaBigDecimal bigDecimalValue = n as JavaBigDecimal;

                if (bigDecimalValue != null)
                {
                    return !bigDecimalValue.equals(BIG_DECIMAL_0);
                }

                return (n.intValue() != 0);
            }
            public static sbyte ToTinyInt(JavaNumber n)
            {
                if (n is JavaByte)
                {
                    return (sbyte) n.intValue();
                }

                const int max = 127;
                const int min = -128;

                if (n is JavaShort
                    || n is JavaInteger)
                {
                    int value = n.intValue();

                    if (max < value || value < min)
                    {
                        throw HsqlConvert.NumericValueOutOfRange(n);
                    }

                    return (sbyte) value;
                }
                else if (n is JavaLong)
                {
                    long value = n.longValue();

                    if (max < value || value < min)
                    {
                        throw HsqlConvert.NumericValueOutOfRange(n);
                    }

                    return (sbyte)value;
                }
                else if (n is JavaDouble || n is JavaFloat)
                {
                    double value = n.doubleValue();

                    if (JavaDouble.isNaN(value)
                        || JavaDouble.isInfinite(value)
                        || max < value
                        || value < min)
                    {
                        throw NumericValueOutOfRange(n);
                    }

                    return (sbyte)value;
                }

                JavaBigDecimal bigDecimalValue = n as JavaBigDecimal;

                if (bigDecimalValue != null)
                {
                    JavaBigInteger bi = bigDecimalValue.toBigInteger();

                    if (bi.compareTo(MAX_TINYINT) > 0
                        || bi.compareTo(MIN_TINYINT) < 0)
                    {
                        throw HsqlConvert.NumericValueOutOfRange(n);
                    }

                    return (sbyte) bi.intValue();
                }

                long longVaue = n.longValue();

                if (max < longVaue || longVaue < min)
                {
                    throw HsqlConvert.NumericValueOutOfRange(n);
                }

                return (sbyte)longVaue;
            }
            public static long ToBigInt(JavaNumber n)
            {
                if (n is JavaByte
                    || n is JavaShort
                    || n is JavaInteger
                    || n is JavaLong)
                {
                    return n.longValue();
                }

                const long max = JavaLong.MAX_VALUE;
                const long min = JavaLong.MIN_VALUE;

                if (n is JavaDouble || n is JavaFloat)
                {
                    double value = n.doubleValue();

                    if (JavaDouble.isNaN(value)
                        || JavaDouble.isInfinite(value)
                        || max < value
                        || value < min)
                    {
                        throw HsqlConvert.NumericValueOutOfRange(n);
                    }

                    return (long)value;
                }

                JavaBigDecimal bigDecimalValue = n as JavaBigDecimal;

                if (bigDecimalValue != null)
                {
                    JavaBigInteger bi = bigDecimalValue.toBigInteger();

                    if (bi.compareTo(MAX_BIGINT) > 0
                        || bi.compareTo(MIN_BIGINT) < 0)
                    {
                        throw HsqlConvert.NumericValueOutOfRange(n);
                    }

                    return bi.longValue();
                }

                return n.longValue();
            }
            public static short ToSmallInt(JavaNumber n)
            {
                if (n is JavaByte
                    || n is JavaShort)
                {
                    return n.shortValue();
                }

                const int max = JavaShort.MAX_VALUE;
                const int min = JavaShort.MIN_VALUE;

                if (n is JavaInteger)
                {
                    int value = n.intValue();

                    if (max < value || value < min)
                    {
                        throw HsqlConvert.NumericValueOutOfRange(n);
                    }

                    return (short) value;
                }

                if (n is JavaLong)
                {
                    long value = n.longValue();

                    if (max < value || value < min)
                    {
                        throw HsqlConvert.NumericValueOutOfRange(n);
                    }

                    return (short) value;
                }

                if (n is JavaDouble || n is JavaFloat)
                {
                    double value = n.doubleValue();

                    if (JavaDouble.isNaN(value)
                        || JavaDouble.isInfinite(value)
                        || max < value
                        || value < min)
                    {
                        throw HsqlConvert.NumericValueOutOfRange(n);
                    }

                    return (short) value;
                }

                JavaBigDecimal bigDecimalValue = n as JavaBigDecimal;

                if (bigDecimalValue != null)
                {
                    JavaBigInteger bi = bigDecimalValue.toBigInteger();

                    if (bi.compareTo(MAX_SMALLINT) > 0
                        || bi.compareTo(MIN_SMALLINT) < 0)
                    {
                        throw HsqlConvert.NumericValueOutOfRange(n);
                    }

                    return bi.shortValue();
                }

                long l = n.longValue();

                if (max < l || l < min)
                {
                    throw HsqlConvert.NumericValueOutOfRange(n);
                }

                return (short) l;
            }