Example usage for java.math BigDecimal toString

List of usage examples for java.math BigDecimal toString

Introduction

In this page you can find the example usage for java.math BigDecimal toString.

Prototype

@Override
public String toString() 

Source Link

Document

Returns the string representation of this BigDecimal , using scientific notation if an exponent is needed.

Usage

From source file:com.micro.rent.common.comm.aio.AsyncClientHttpExchangeFutureCallback.java

public void httpAsync() throws Exception {
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(3000).setConnectTimeout(3000).build();
    final CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom().setDefaultRequestConfig(requestConfig)
            .build();// w ww.  jav a  2 s  .  c  om
    try {
        httpclient.start();
        final HttpGet[] requests = new HttpGet[list.size()];
        for (int i = 0; i < list.size(); i++) {
            BigDecimal lat = list.get(i).getLatitude();
            BigDecimal lon = list.get(i).getLongitude();
            RequestParam reqParam = new RequestParam();
            reqParam.setDestination(lat.toString().concat(",").concat(lon.toString()));
            reqParam.setOrigin(String.valueOf(wpLat).concat(",").concat(String.valueOf(wpLon)));
            reqParam.setMode(queryVo.getTrafficType());
            switch (ETranfficType.getSelfByCode(queryVo.getTrafficType())) {
            case DRIVING:
                reqParam.setOrigin_region(queryVo.getCityName());
                reqParam.setDestination_region(queryVo.getCityName());
                break;
            case TRANSIT:
            case WALKING:
                reqParam.setRegion(queryVo.getCityName());
                break;
            default:
                break;
            }
            requests[i] = new HttpGet(timeUrl(reqParam));
        }
        long start = System.currentTimeMillis();
        final CountDownLatch latch = new CountDownLatch(requests.length);
        for (int j = 0; j < requests.length; j++) {
            final HttpGet request = requests[j];
            final int k = j;
            httpclient.execute(request, new FutureCallback<HttpResponse>() {
                public void completed(final HttpResponse response) {
                    latch.countDown();
                    try {
                        InputStream a = response.getEntity().getContent();
                        String responseString = readInputStream(a);
                        String duration = duration(responseString);
                        list.get(k).setDuration(duration);
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                public void failed(final Exception ex) {
                    latch.countDown();
                }

                public void cancelled() {
                    latch.countDown();
                }

            });
        }
        latch.await();
        log.info(System.currentTimeMillis() - start + "-----------------ms----");
    } finally {
        httpclient.close();
    }
}

From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.Swing.CFJFloatEditor.java

public boolean isEditValid() {
    final String S_ProcName = "isEditValid";
    if (!hasValue()) {
        setValue(null);/*  w  w  w . j a  v a  2 s  . c o  m*/
        return (true);
    }

    boolean retval = super.isEditValid();
    if (retval) {
        try {
            commitEdit();
        } catch (ParseException e) {
            throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                    "Field is not valid - " + e.getMessage(), e);

        }
        Object obj = getValue();
        if (obj == null) {
            retval = false;
        } else if (obj instanceof Float) {
            Float v = (Float) obj;
            Float min = getMinValue();
            Float max = getMaxValue();
            if ((v.compareTo(min) < 0) || (v.compareTo(max) > 0)) {
                retval = false;
            }
        } else if (obj instanceof Double) {
            Double v = (Double) obj;
            Double min = getMinValue().doubleValue();
            Double max = getMaxValue().doubleValue();
            if ((v.compareTo(min) < 0) || (v.compareTo(max) > 0)) {
                retval = false;
            }
        } else if (obj instanceof Short) {
            Short s = (Short) obj;
            Float v = new Float(s.floatValue());
            Float min = getMinValue();
            Float max = getMaxValue();
            if ((v.compareTo(min) < 0) || (v.compareTo(max) > 0)) {
                retval = false;
            }
        } else if (obj instanceof Integer) {
            Integer i = (Integer) obj;
            Float v = new Float(i.floatValue());
            Float min = getMinValue();
            Float max = getMaxValue();
            if ((v.compareTo(min) < 0) || (v.compareTo(max) > 0)) {
                retval = false;
            }
        } else if (obj instanceof Long) {
            Long l = (Long) obj;
            Float v = new Float(l.floatValue());
            Float min = getMinValue();
            Float max = getMaxValue();
            if ((v.compareTo(min) < 0) || (v.compareTo(max) > 0)) {
                retval = false;
            }
        } else if (obj instanceof BigDecimal) {
            BigDecimal b = (BigDecimal) obj;
            Float v = new Float(b.toString());
            Float min = getMinValue();
            Float max = getMaxValue();
            if ((v.compareTo(min) < 0) || (v.compareTo(max) > 0)) {
                retval = false;
            }
        } else if (obj instanceof Number) {
            Number n = (Number) obj;
            Float v = new Float(n.toString());
            Float min = getMinValue();
            Float max = getMaxValue();
            if ((v.compareTo(min) < 0) || (v.compareTo(max) > 0)) {
                retval = false;
            }
        } else {
            throw CFLib.getDefaultExceptionFactory().newUnsupportedClassException(getClass(), S_ProcName,
                    "EditedValue", obj, "Short, Integer, Long, BigDecimal, Float, Double or Number");
        }
    }
    return (retval);
}

From source file:com.taobao.tdhs.jdbc.TDHSPreparedStatement.java

public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException {
    setDString(parameterIndex, x == null ? null : x.toString());
}

From source file:edu.macalester.tagrelatedness.KendallsCorrelation.java

public BigDecimal get(BigDecimal n) {

    // Make sure n is a positive number

    if (n.compareTo(ZERO) <= 0) {
        throw new IllegalArgumentException();
    }/*ww  w.  j  a va 2 s . c  o m*/

    BigDecimal initialGuess = getInitialApproximation(n);
    trace("Initial guess " + initialGuess.toString());
    BigDecimal lastGuess = ZERO;
    BigDecimal guess = new BigDecimal(initialGuess.toString());

    // Iterate

    iterations = 0;
    boolean more = true;
    while (more) {
        lastGuess = guess;
        guess = n.divide(guess, scale, BigDecimal.ROUND_HALF_UP);
        guess = guess.add(lastGuess);
        guess = guess.divide(TWO, scale, BigDecimal.ROUND_HALF_UP);
        trace("Next guess " + guess.toString());
        error = n.subtract(guess.multiply(guess));
        if (++iterations >= maxIterations) {
            more = false;
        } else if (lastGuess.equals(guess)) {
            more = error.abs().compareTo(ONE) >= 0;
        }
    }
    return guess;

}

From source file:net.sourceforge.msscodefactory.cfacc.v2_0.CFAccMssCF.CFAccMssCFBindAccountEntryConvertedAmount.java

public String expandBody(MssCFGenContext genContext) {
    final String S_ProcName = "CFAccMssCFBindAccountEntryConvertedAmount.expandBody() ";

    if (genContext == null) {
        throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), "expandBody", 1,
                "genContext");
    }//from  w ww .  java 2  s .co m

    ICFLibAnyObj genDef = genContext.getGenDef();
    if (genDef == null) {
        throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), "expandBody", 1,
                "genContext.getGenDef()");
    }

    String ret;

    if (genDef instanceof ICFAccAccountEntryObj) {
        BigDecimal convertedAmount = ((ICFAccAccountEntryObj) genDef).getRequiredConvertedAmount();
        if (convertedAmount == null) {
            throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), "expandBody", 0,
                    "Value");
        }
        ret = convertedAmount.toString();
    } else {
        throw CFLib.getDefaultExceptionFactory().newUnsupportedClassException(getClass(), "expandBody",
                "genContext.getGenDef()", genDef, "ICFAccAccountEntryObj");
    }

    return (ret);
}

From source file:net.sourceforge.msscodefactory.cfgcash.v2_0.CFGCashMssCF.CFGCashMssCFBindAccountEntryConvertedAmount.java

public String expandBody(MssCFGenContext genContext) {
    final String S_ProcName = "CFGCashMssCFBindAccountEntryConvertedAmount.expandBody() ";

    if (genContext == null) {
        throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), "expandBody", 1,
                "genContext");
    }/* w  ww. j a v  a  2  s . co  m*/

    ICFLibAnyObj genDef = genContext.getGenDef();
    if (genDef == null) {
        throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), "expandBody", 1,
                "genContext.getGenDef()");
    }

    String ret;

    if (genDef instanceof ICFGCashAccountEntryObj) {
        BigDecimal convertedAmount = ((ICFGCashAccountEntryObj) genDef).getRequiredConvertedAmount();
        if (convertedAmount == null) {
            throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), "expandBody", 0,
                    "Value");
        }
        ret = convertedAmount.toString();
    } else {
        throw CFLib.getDefaultExceptionFactory().newUnsupportedClassException(getClass(), "expandBody",
                "genContext.getGenDef()", genDef, "ICFGCashAccountEntryObj");
    }

    return (ret);
}

From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.Swing.CFJFloatEditor.java

public Float getFloatValue() {
    final String S_ProcName = "getFloatValue";
    Float retval;//from w w w.j a  v  a 2s .com
    String text = getText();
    if ((text == null) || (text.length() <= 0)) {
        retval = null;
    } else {
        if (!isEditValid()) {
            throw CFLib.getDefaultExceptionFactory().newInvalidArgumentException(getClass(), S_ProcName,
                    "Field is not valid");
        }
        try {
            commitEdit();
        } catch (ParseException e) {
            throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                    "Field is not valid - " + e.getMessage(), e);

        }
        Object obj = getValue();
        if (obj == null) {
            retval = null;
        } else if (obj instanceof Float) {
            Float v = (Float) obj;
            Float min = getMinValue();
            Float max = getMaxValue();
            if ((v.compareTo(min) < 0) || (v.compareTo(max) > 0)) {
                throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(getClass(), S_ProcName, 0,
                        "EditedValue", v, min, max);
            }
            retval = v;
        } else if (obj instanceof Double) {
            Double v = (Double) obj;
            Double min = getMinValue().doubleValue();
            Double max = getMaxValue().doubleValue();
            if ((v.compareTo(min) < 0) || (v.compareTo(max) > 0)) {
                throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(getClass(), S_ProcName, 0,
                        "EditedValue", v, min, max);
            }
            retval = new Float(v.floatValue());
        } else if (obj instanceof Short) {
            Short s = (Short) obj;
            Float v = new Float(s.floatValue());
            Float min = getMinValue();
            Float max = getMaxValue();
            if ((v.compareTo(min) < 0) || (v.compareTo(max) > 0)) {
                throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(getClass(), S_ProcName, 0,
                        "EditedValue", v, min, max);
            }
            retval = v;
        } else if (obj instanceof Integer) {
            Integer i = (Integer) obj;
            Float v = new Float(i.floatValue());
            Float min = getMinValue();
            Float max = getMaxValue();
            if ((v.compareTo(min) < 0) || (v.compareTo(max) > 0)) {
                throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(getClass(), S_ProcName, 0,
                        "EditedValue", v, min, max);
            }
            retval = v;
        } else if (obj instanceof Long) {
            Long l = (Long) obj;
            Float v = new Float(l.floatValue());
            Float min = getMinValue();
            Float max = getMaxValue();
            if ((v.compareTo(min) < 0) || (v.compareTo(max) > 0)) {
                throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(getClass(), S_ProcName, 0,
                        "EditedValue", v, min, max);
            }
            retval = v;
        } else if (obj instanceof BigDecimal) {
            BigDecimal b = (BigDecimal) obj;
            Float v = new Float(b.toString());
            Float min = getMinValue();
            Float max = getMaxValue();
            if ((v.compareTo(min) < 0) || (v.compareTo(max) > 0)) {
                throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(getClass(), S_ProcName, 0,
                        "EditedValue", v, min, max);
            }
            retval = v;
        } else if (obj instanceof Number) {
            Number n = (Number) obj;
            Float v = new Float(n.toString());
            Float min = getMinValue();
            Float max = getMaxValue();
            if ((v.compareTo(min) < 0) || (v.compareTo(max) > 0)) {
                throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(getClass(), S_ProcName, 0,
                        "EditedValue", v, min, max);
            }
            retval = v;
        } else {
            throw CFLib.getDefaultExceptionFactory().newUnsupportedClassException(getClass(), S_ProcName,
                    "EditedValue", obj, "Short, Integer, Long, BigDecimal or Number");
        }
    }
    return (retval);
}

From source file:pe.gob.mef.gescon.service.impl.ConsultaServiceImpl.java

@Override
public List<HashMap<String, Object>> listarReporte(HashMap filters) {
    List<HashMap<String, Object>> lista = new ArrayList<HashMap<String, Object>>();
    try {//from   www  .  j ava2s .c o  m
        ConsultaDao consultaDao = (ConsultaDao) ServiceFinder.findBean("ConsultaDao");
        List<HashMap<String, Object>> consulta = consultaDao.listarReporte(filters);
        if (!CollectionUtils.isEmpty(consulta)) {
            for (HashMap<String, Object> r : consulta) {
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("ID", r.get("ID"));
                map.put("NOMBRE", r.get("NOMBRE"));
                map.put("SUMILLA", r.get("SUMILLA"));
                map.put("FECHA", r.get("FECHA"));
                map.put("IDCATEGORIA", r.get("IDCATEGORIA"));
                map.put("CATEGORIA", r.get("CATEGORIA"));
                map.put("IDTIPOCONOCIMIENTO", r.get("IDTIPOCONOCIMIENTO"));
                map.put("TIPOCONOCIMIENTO", r.get("TIPOCONOCIMIENTO"));
                map.put("IDESTADO", r.get("IDESTADO"));
                map.put("ESTADO", r.get("ESTADO"));
                map.put("PROMEDIO", r.get("PROMEDIO"));
                map.put("USUARIOCREA", r.get("USUARIOCREA"));
                map.put("FECHACREA", r.get("FECHACREA"));
                BigDecimal contador = (BigDecimal) r.get("CONTADOR");
                BigDecimal suma = (BigDecimal) r.get("SUMA");

                if (BigDecimal.ZERO.equals(contador)) {
                    map.put("CONTADOR", 0);
                } else {
                    int calificacion = Math
                            .round(Float.parseFloat(suma.toString()) / Integer.parseInt(contador.toString()));
                    map.put("CONTADOR", calificacion);
                }
                lista.add(map);
            }
        }
    } catch (Exception e) {
        e.getMessage();
        e.printStackTrace();
    }
    return lista;
}

From source file:org.getwheat.harvest.library.dom.DomHelper.java

/**
 * Adds an {@link Element} to the parent if the value is not null.
 * <p />//from   w  w  w.  j a va 2s .c  o m
 * A type attribute is also added to the {@link Element}.
 * 
 * @param document
 * @param parent
 * @param tag
 * @param value
 */
public void addElement(final Document document, final Element parent, final XmlTag tag,
        final BigDecimal value) {
    if (value != null) {
        final Element element = addElement(document, parent, tag, value.toString());
        element.setAttribute(VALUE_TYPE, VALUE_DECIMAL);
    }
}

From source file:org.dbrain.data.jackson.serializers.JsonBigDecimalSerializer.java

@Override
public void serialize(BigDecimal value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
    if (value != null) {
        // Does it have more that 15 significant digits ?
        BigInteger unscaled = value.unscaledValue();
        if (unscaled.compareTo(MIN_VALUE) >= 0 && unscaled.compareTo(MAX_VALUE) <= 0) {
            jgen.writeNumber(value);//from   w w  w  . j  a v  a 2  s  .com
        } else {
            jgen.writeString(value.toString());
        }
    } else {
        jgen.writeNull();
    }
}