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:org.jumpmind.symmetric.io.data.transform.AdditiveColumnTransform.java

public String transform(IDatabasePlatform platform, DataContext context, TransformColumn column,
        TransformedData data, Map<String, String> sourceValues, String newValue, String oldValue)
        throws IgnoreColumnException, IgnoreRowException {

    BigDecimal multiplier = new BigDecimal(1.00);

    if (StringUtils.isNotBlank(column.getTransformExpression())) {
        multiplier = new BigDecimal(column.getTransformExpression());
    }//  w  ww .  j a v a2 s.c om

    Table table = platform.getTableFromCache(data.getCatalogName(), data.getSchemaName(), data.getTableName(),
            false);
    if (table == null) {
        if (log.isDebugEnabled()) {
            log.debug("Could not find the target table {}", data.getFullyQualifiedTableName());
        }
        throw new IgnoreColumnException();
    } else if (table.getColumnWithName(column.getTargetColumnName()) == null) {
        if (log.isDebugEnabled()) {
            log.debug("Could not find the target column {}", column.getTargetColumnName());
        }
        throw new IgnoreColumnException();
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Old, new, transform expression as received: " + oldValue + ", " + newValue + ", "
                    + column.getTransformExpression());
        }

        if (!StringUtils.isNotBlank(newValue) || data.getSourceDmlType() == DataEventType.DELETE) {
            newValue = "0";
        }
        if (!StringUtils.isNotBlank(oldValue)) {
            oldValue = "0";
        }

        BigDecimal delta = new BigDecimal(newValue);
        delta = delta.subtract(new BigDecimal(oldValue));
        delta = delta.multiply(multiplier);
        newValue = delta.toString();

        String quote = platform.getDdlBuilder().isDelimitedIdentifierModeOn()
                ? platform.getDatabaseInfo().getDelimiterToken()
                : "";
        StringBuilder sql = new StringBuilder(String.format("update %s set %s=%s+(%s) where ",
                getFullyQualifiedTableName(platform, data.getSchemaName(), data.getCatalogName(),
                        data.getTableName()),
                quote + column.getTargetColumnName() + quote, quote + column.getTargetColumnName() + quote,
                newValue));

        String[] keyNames = data.getKeyNames();
        List<Column> columns = new ArrayList<Column>();
        List<String> keyValuesList = new ArrayList<String>();
        boolean addedFirstKey = false;
        for (int i = 0; i < keyNames.length; i++) {
            Column targetCol = table.getColumnWithName(keyNames[i]);
            if (targetCol != null) {
                columns.add(targetCol);
                keyValuesList.add(sourceValues.get(keyNames[i]));
                if (addedFirstKey) {
                    sql.append("and ");
                } else {
                    addedFirstKey = true;
                }
                sql.append(quote);
                sql.append(keyNames[i]);
                sql.append(quote);
                sql.append("=? ");
            }
        }

        if (log.isDebugEnabled()) {
            log.debug("SQL: " + sql);
        }
        ISqlTransaction transaction = context.findTransaction();
        if (0 < transaction.prepareAndExecute(sql.toString(),
                platform.getObjectValues(context.getBatch().getBinaryEncoding(),
                        keyValuesList.toArray(new String[keyValuesList.size()]),
                        columns.toArray(new Column[columns.size()])))) {
            throw new IgnoreColumnException();
        }

    }

    return newValue;
}

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

public Double getDoubleValue() {
    final String S_ProcName = "getDoubleValue";
    Double retval;/*from www . ja v  a  2s .c o m*/
    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 f = (Float) obj;
            Double v = new Double(f.doubleValue());
            if ((v.compareTo(minValue) < 0) || (v.compareTo(maxValue) > 0)) {
                throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(getClass(), S_ProcName, 0,
                        "EditedValue", v, minValue, maxValue);
            }
            retval = v;
        } else if (obj instanceof Double) {
            Double v = (Double) obj;
            if ((v.compareTo(minValue) < 0) || (v.compareTo(maxValue) > 0)) {
                throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(getClass(), S_ProcName, 0,
                        "EditedValue", v, minValue, maxValue);
            }
            retval = v;
        } else if (obj instanceof Short) {
            Short s = (Short) obj;
            Double v = new Double(s.doubleValue());
            if ((v.compareTo(minValue) < 0) || (v.compareTo(maxValue) > 0)) {
                throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(getClass(), S_ProcName, 0,
                        "EditedValue", v, minValue, maxValue);
            }
            retval = v;
        } else if (obj instanceof Integer) {
            Integer i = (Integer) obj;
            Double v = new Double(i.doubleValue());
            if ((v.compareTo(minValue) < 0) || (v.compareTo(maxValue) > 0)) {
                throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(getClass(), S_ProcName, 0,
                        "EditedValue", v, minValue, maxValue);
            }
            retval = v;
        } else if (obj instanceof Long) {
            Long l = (Long) obj;
            Double v = new Double(l.doubleValue());
            if ((v.compareTo(minValue) < 0) || (v.compareTo(maxValue) > 0)) {
                throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(getClass(), S_ProcName, 0,
                        "EditedValue", v, minValue, maxValue);
            }
            retval = v;
        } else if (obj instanceof BigDecimal) {
            BigDecimal b = (BigDecimal) obj;
            Double v = new Double(b.toString());
            if ((v.compareTo(minValue) < 0) || (v.compareTo(maxValue) > 0)) {
                throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(getClass(), S_ProcName, 0,
                        "EditedValue", v, minValue, maxValue);
            }
            retval = v;
        } else if (obj instanceof Number) {
            Number n = (Number) obj;
            Double v = new Double(n.toString());
            if ((v.compareTo(minValue) < 0) || (v.compareTo(maxValue) > 0)) {
                throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(getClass(), S_ProcName, 0,
                        "EditedValue", v, minValue, maxValue);
            }
            retval = v;
        } else {
            throw CFLib.getDefaultExceptionFactory().newUnsupportedClassException(getClass(), S_ProcName,
                    "EditedValue", obj, "Short, Integer, Long, BigDecimal or Number");
        }
    }
    return (retval);
}

From source file:org.talend.dataprofiler.chart.preview.DQRuleItemLabelGenerator.java

/**
 * DOC yyin Comment method "stringformat".
 * /*from   w  w w  .j  a v a2s.co  m*/
 * @param percent
 * @param i
 * @return
 */
private Object stringformat(Object percent, int i) {
    // ADD msjian TDQ-10793: when there is no data, the percent value is NaN
    if (Double.isNaN((double) percent)) {
        return String.valueOf(Double.NaN);
    }
    // TDQ-10793~

    BigDecimal zero = new BigDecimal(0);
    BigDecimal temp = new BigDecimal(percent.toString());
    BigDecimal min = new BigDecimal(10E-5);
    BigDecimal max = new BigDecimal(9999 * 10E-5);
    boolean isUseScientific = false;
    if (temp.compareTo(min) == -1 && temp.compareTo(zero) == 1) {
        isUseScientific = true;
    } else if (temp.compareTo(max) == 1 && temp.compareTo(new BigDecimal(1)) == -1) {
        percent = max.toString();
    }
    DecimalFormat format = (DecimalFormat) DecimalFormat.getPercentInstance(Locale.ENGLISH);
    format.applyPattern("0.00%"); //$NON-NLS-1$

    if (isUseScientific) {
        format.applyPattern("0.###E0%"); //$NON-NLS-1$
    }
    return format.format(new Double(percent.toString()));
}

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

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

    if (genContext == null) {
        throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), "expandBody", 1,
                "genContext");
    }//from www  .  ja v a2s. c  om

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

    String ret;

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

    return (ret);
}

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

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

    if (genContext == null) {
        throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), "expandBody", 1,
                "genContext");
    }/*  ww  w  .  j a  va2s. c  o  m*/

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

    String ret;

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

    return (ret);
}

From source file:org.osiam.resource_server.storage.helper.NumberPadder.java

/**
 * Removes the offset and padding from a number
 *
 * @param value/*from w w  w.  j  a  v  a  2s  . c o m*/
 *            the padded number as {@link String}
 * @return the number decreased by offset and leading '0's removed
 */
public String unpad(String value) {
    BigDecimal decimalValue = new BigDecimal(value);
    BigDecimal returnDecimalValue = new BigDecimal(decimalValue.toBigInteger().subtract(BIG_OFFSET));
    BigDecimal signum = new BigDecimal(returnDecimalValue.signum());
    BigDecimal fractionalPart = decimalValue.remainder(BigDecimal.ONE).multiply(signum);
    returnDecimalValue = returnDecimalValue.add(fractionalPart);
    return returnDecimalValue.toString();
}

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

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

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

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

    String ret;

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

    return (ret);
}

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

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

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

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

    String ret;

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

    return (ret);
}

From source file:org.hoteia.qalingo.core.solr.service.ProductSkuSolrService.java

public void addOrUpdateProductSku(final ProductMarketing productMarketing, final ProductSku productSku,
        final List<CatalogCategoryVirtual> catalogCategories, final MarketArea marketArea,
        final Retailer retailer) throws SolrServerException, IOException {
    if (productSku.getId() == null) {
        throw new IllegalArgumentException("Id  cannot be blank or null.");
    }//from   w  ww  . ja v a2s.c om
    if (logger.isDebugEnabled()) {
        logger.debug("Indexing productSku " + productSku.getId() + " : " + productSku.getCode() + " : "
                + productSku.getName());
    }
    ProductSkuSolr productSkuSolr = new ProductSkuSolr();
    productSkuSolr.setId(productSku.getId());
    productSkuSolr.setCode(productSku.getCode());
    productSkuSolr.setName(productSku.getName());
    productSkuSolr.setDescription(productSku.getDescription());

    productSkuSolr.setEnabledB2B(productSku.isEnabledB2B());
    productSkuSolr.setEnabledB2C(productSku.isEnabledB2C());

    productSkuSolr.setSalableB2B(productSku.isSalableB2B());
    productSkuSolr.setSalableB2C(productSku.isSalableB2C());

    if (productMarketing.getProductBrand() != null
            && Hibernate.isInitialized(productMarketing.getProductBrand())) {
        productSkuSolr.setProductBrandCode(productMarketing.getProductBrand().getCode());
        productSkuSolr.setProductBrandName(productMarketing.getProductBrand().getName());
    }

    CatalogCategoryVirtual defaultVirtualCatalogCategory = productService
            .getDefaultVirtualCatalogCategory(productSku, catalogCategories, true);

    if (defaultVirtualCatalogCategory != null) {
        productSkuSolr.setDefaultCategoryCode(defaultVirtualCatalogCategory.getCode());
    }

    if (catalogCategories != null) {
        for (CatalogCategoryVirtual catalogCategoryVirtual : catalogCategories) {
            String catalogCode = catalogCategoryVirtual.getCatalog().getCode();
            productSkuSolr.addCatalogCode(catalogCode);
            String catalogCategoryCode = catalogCode + "_" + catalogCategoryVirtual.getCode();
            productSkuSolr.addCatalogCategories(catalogCategoryCode);
        }
    }

    if (productSku.getOptionRels() != null && Hibernate.isInitialized(productSku.getOptionRels())
            && !productSku.getOptionRels().isEmpty()) {
        for (ProductSkuOptionRel productSkuOptionRel : productSku.getOptionRels()) {
            ProductSkuOptionDefinition productSkuOptionDefinition = productSkuOptionRel
                    .getProductSkuOptionDefinition();
            productSkuSolr.addOptionDefinition(productSkuOptionDefinition.getCode());
        }
    }

    if (productSku.getTagRels() != null && Hibernate.isInitialized(productSku.getTagRels())
            && !productSku.getTagRels().isEmpty()) {
        for (ProductSkuTagRel productSkuTagRel : productSku.getTagRels()) {
            Tag productSkuTag = productSkuTagRel.getProductSkuTag();
            productSkuSolr.addTag(productSkuTag.getCode());
        }
    }

    if (marketArea != null && retailer != null) {
        ProductSkuPrice productSkuPrice = productSku.getBestPrice(marketArea.getId());
        if (productSkuPrice != null) {
            BigDecimal salePrice = productSkuPrice.getSalePrice();
            productSkuSolr.setPrice(salePrice.toString());
        }
    }

    productSkuSolrServer.addBean(productSkuSolr);
    productSkuSolrServer.commit();
}

From source file:com.willetinc.hadoop.mapreduce.dynamodb.BigDecimalSplitter.java

@Override
void generateRangeKeySplits(Configuration conf, List<InputSplit> splits, Types hashKeyType,
        AttributeValue hashKeyValue, Types rangeKeyType, AttributeValue minRangeKeyValue,
        AttributeValue maxRangeKeyValue, int numRangeSplits) {

    BigDecimal numSplits = BigDecimal.valueOf(numRangeSplits);
    BigDecimal minVal = new BigDecimal(minRangeKeyValue.getN());
    BigDecimal maxVal = new BigDecimal(maxRangeKeyValue.getN());

    // Get all the split points together.
    List<BigDecimal> splitPoints = split(numSplits, minVal, maxVal);

    // Turn the split points into a set of intervals.
    BigDecimal start = splitPoints.get(0);
    for (int i = 1; i < splitPoints.size(); i++) {
        BigDecimal end = splitPoints.get(i);

        List<AttributeValue> rangeKeyValues = new ArrayList<AttributeValue>();
        rangeKeyValues.add(new AttributeValue().withN(start.toString()));
        rangeKeyValues.add(new AttributeValue().withN(end.toString()));

        splits.add(new DynamoDBQueryInputFormat.DynamoDBQueryInputSplit(hashKeyType, hashKeyValue, rangeKeyType,
                rangeKeyValues, ComparisonOperator.BETWEEN));

        // set start to end of last interval plus minimum positive value
        // in the case of DynamoDB Numbers it is 1.0^-38:
        // This is necessary to ensure we don't miss any values between
        // intervals.
        start = end.add(MIN_POSITIVE_VALUE);
    }/*from www .ja  v  a2 s. co  m*/
}