Package java.math

Examples of java.math.BigDecimal


        } else if (clazz == Double.class) {
            return (T) new Double(COUNTER.getAndIncrement());
        } else if (clazz == Boolean.class) {
            return (T) new Boolean(false);
        } else if (clazz == BigDecimal.class) {
            return (T) new BigDecimal(COUNTER.getAndIncrement());
        } else if (clazz == BigInteger.class) {
            return (T) new BigInteger("" + COUNTER.getAndIncrement());
        } else if (clazz == java.sql.Date.class) {
            return (T) new java.sql.Date(COUNTER.getAndIncrement());
        } else if (clazz == java.sql.Time.class) {
View Full Code Here


        assertTrue(rs.getBoolean(3));
        assertTrue(rs.getBoolean("c"));
        assertEquals(d.getTime(), rs.getDate(4).getTime());
        assertEquals(d.getTime(), rs.getDate("d").getTime());

        assertTrue(new BigDecimal("10.3").equals(rs.getBigDecimal(5)));
        assertTrue(new BigDecimal("10.3").equals(rs.getBigDecimal("e")));
        assertEquals(10.3, rs.getDouble(5));
        assertEquals((float) 10.3, rs.getFloat(5));

        assertTrue(Math.PI == rs.getDouble(6));
        assertTrue(Math.PI == rs.getDouble("f"));
View Full Code Here

        Statement stat = conn.createStatement();
        stat.execute("create table test(id int primary key, name varchar, amount decimal)");
        PreparedStatement prep = conn.prepareStatement("insert into test values(?, ?, ?)");
        prep.setInt(1, 1);
        prep.setString(2, "Hello \\'Joe\n\\'");
        prep.setBigDecimal(3, new BigDecimal("10.20"));
        prep.executeUpdate();
        stat.execute("create table test2(id int primary key,\n" +
                "a real, b double, c bigint,\n" +
                "d smallint, e boolean, f binary, g date, h time, i timestamp)", Statement.NO_GENERATED_KEYS);
        prep = conn.prepareStatement("insert into test2 values(1, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
View Full Code Here

            else if (Float.TYPE.equals(clazz))
                return number.floatValue();
            else if (Float.class.equals(clazz))
                return new Float(number.floatValue());
            else if (BigDecimal.class.equals(clazz))
                return new BigDecimal(number.doubleValue());
            else if (BigInteger.class.equals(clazz))
                return new BigInteger(number.toString());
           // else
            // TODO log warn
             
        } else if (isDateTime(clazz)) {
          return handleAsDate(clazz,value,customizeInfo,method);
        } else if (clazz.isEnum()) {
            String sValue = (value instanceof Map) ? ((String)((Map)value).get("_name")) : (String) value;
            try{
              return Enum.valueOf(clazz, sValue);
            }catch(Throwable th){
              // try in value fields
              for(Object conzt: clazz.getEnumConstants()){
            try{
              // If xml value annotation use it.
              if(clazz.getDeclaredField(((Enum<?>)conzt).name()).
                  getAnnotation(XmlEnumValue.class).value().equals(sValue)){
                return conzt;
              }
            }catch(Throwable t){};
          }
              // user error?
              return null;
            }
        } else if (value instanceof String) {
            String sValue = (String) value;
            if(sValue.trim().isEmpty() && Number.class.isAssignableFrom(clazz)){
              sValue = "0";
              if(traceEnabled)traceLog.warn("Empty string passed for number. Converting it to zero. Field : " + method.getName());
            }
            if (String.class.equals(clazz))
              return value;
            else if (Boolean.TYPE.equals(clazz))
                return Boolean.parseBoolean(sValue);
            else if (Boolean.class.equals(clazz))
                return Boolean.valueOf(sValue);
            else if (Short.TYPE.equals(clazz)   || Short.class.equals(clazz))
                return Short.decode(sValue);
            else if (Byte.TYPE.equals(clazz)   || Byte.class.equals(clazz))
                return Byte.decode(sValue);
            else if (Integer.TYPE.equals(clazz) || Integer.class.equals(clazz))
                return Integer.decode(sValue);
            else if (Long.TYPE.equals(clazz|| Long.class.equals(clazz))
                return Long.decode(sValue);
            else if (Float.TYPE.equals(clazz))
                return Float.parseFloat(sValue);
            else if (Float.class.equals(clazz))
                return Float.valueOf(sValue);
            else if (Double.TYPE.equals(clazz))
                return Double.parseDouble(sValue);
            else if (Double.class.equals(clazz))
                return Double.valueOf(sValue);
            else if (BigDecimal.class.equals(clazz))
                return new BigDecimal(sValue);
            else if (BigInteger.class.equals(clazz))
                return new BigInteger(sValue);
            else if (Character.TYPE.equals(clazz) || Character.class.equals(clazz)) {
                char charValue = 0;
                if (sValue.length() > 0) {
View Full Code Here

        assertEquals(10, v.convertPrecision(10, false).getPrecision());
        assertEquals(10, v.convertPrecision(10, false).getBytes().length);
        assertEquals(32, v.convertPrecision(10, false).getBytes()[9]);
        assertEquals(10, v.convertPrecision(10, true).getPrecision());

        final Value vd = ValueDecimal.get(new BigDecimal("1234567890.123456789"));
        assertEquals(19, vd.getPrecision());
        assertEquals("1234567890.1234567", vd.convertPrecision(10, true).getString());
        new AssertThrows(ErrorCode.NUMERIC_VALUE_OUT_OF_RANGE_1) { public void test() {
            vd.convertPrecision(10, false);
        }};
View Full Code Here

        ValueDouble vd3 = vd1.modulus(vd2);
        assertEquals(2, vd3.getDouble());
    }

    private void testModulusDecimal() {
        final ValueDecimal vd1 = ValueDecimal.get(new BigDecimal(12));
        new AssertThrows(ErrorCode.DIVISION_BY_ZERO_1) { public void test() {
            vd1.modulus(ValueDecimal.get(new BigDecimal(0)));
        }};
        ValueDecimal vd2 = ValueDecimal.get(new BigDecimal(10));
        ValueDecimal vd3 = vd1.modulus(vd2);
        assertEquals(2, vd3.getDouble());
    }
View Full Code Here

        }
        buff.insert(len - scale, '.');
        if (config.random().getBoolean(20)) {
            buff.insert(0, '-');
        }
        return new BigDecimal(buff.toString());
    }
View Full Code Here

        testValue(ValueDecimal.get(BigDecimal.ONE));
        testValue(ValueDecimal.get(BigDecimal.TEN));
        testValue(ValueDecimal.get(BigDecimal.ONE.negate()));
        testValue(ValueDecimal.get(BigDecimal.TEN.negate()));
        for (long i = 0; i >= 0; i += 10 + i / 4) {
            testValue(ValueDecimal.get(new BigDecimal(i)));
            testValue(ValueDecimal.get(new BigDecimal(-i)));
            for (int j = 0; j < 200; j += 50) {
                testValue(ValueDecimal.get(new BigDecimal(i).setScale(j)));
                testValue(ValueDecimal.get(new BigDecimal(i * i).setScale(j)));
            }
            testValue(ValueDecimal.get(new BigDecimal(i * i)));
        }
        testValue(ValueDate.get(new Date(System.currentTimeMillis())));
        testValue(ValueDate.get(new Date(0)));
        testValue(ValueTime.get(new Time(System.currentTimeMillis())));
        testValue(ValueTime.get(new Time(0)));
View Full Code Here

        }
    }

    private void testParseSpecialValue(String x) throws SQLException {
        Object expected;
        expected = new BigDecimal(x);
        try {
            expected = Long.decode(x);
            expected = Integer.decode(x);
        } catch (Exception e) {
            // ignore
View Full Code Here

        stat.execute("INSERT INTO TEST VALUES(7,-99999998.99)");
        stat.execute("INSERT INTO TEST VALUES(8,NULL)");
        rs = stat.executeQuery("SELECT * FROM TEST ORDER BY ID");
        assertResultSetMeta(rs, 2, new String[] { "ID", "VALUE" }, new int[] { Types.INTEGER, Types.DECIMAL }, new int[] {
                10, 10 }, new int[] { 0, 2 });
        BigDecimal bd;
        rs.next();
        assertTrue(rs.getInt(1) == 1);
        assertTrue(!rs.wasNull());
        assertTrue(rs.getInt(2) == -1);
        assertTrue(!rs.wasNull());
        bd = rs.getBigDecimal(2);
        assertTrue(bd.compareTo(new BigDecimal("-1.00")) == 0);
        assertTrue(!rs.wasNull());
        o = rs.getObject(2);
        trace(o.getClass().getName());
        assertTrue(o instanceof BigDecimal);
        assertTrue(((BigDecimal) o).compareTo(new BigDecimal("-1.00")) == 0);
        rs.next();
        assertTrue(rs.getInt(1) == 2);
        assertTrue(!rs.wasNull());
        assertTrue(rs.getInt(2) == 0);
        assertTrue(!rs.wasNull());
        bd = rs.getBigDecimal(2);
        assertTrue(bd.compareTo(new BigDecimal("0.00")) == 0);
        assertTrue(!rs.wasNull());
        rs.next();
        checkColumnBigDecimal(rs, 2, 1, "1.00");
        rs.next();
        checkColumnBigDecimal(rs, 2, 12345679, "12345678.89");
View Full Code Here

TOP

Related Classes of java.math.BigDecimal

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.