Example usage for java.math BigDecimal signum

List of usage examples for java.math BigDecimal signum

Introduction

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

Prototype

public int signum() 

Source Link

Document

Returns the signum function of this BigDecimal .

Usage

From source file:net.pms.util.Rational.java

/**
 * Returns an instance that represents the value of {@code value}.
 *
 * @param value the value.//w w w  . j av a 2 s.  com
 * @return An instance that represents the value of {@code value}.
 */
@Nullable
public static Rational valueOf(@Nullable BigDecimal value) {
    if (value == null) {
        return null;
    }
    BigInteger numerator;
    BigInteger denominator;
    if (value.signum() == 0) {
        return ZERO;
    }
    if (BigDecimal.ONE.equals(value)) {
        return ONE;
    }
    if (value.scale() > 0) {
        BigInteger unscaled = value.unscaledValue();
        BigInteger tmpDenominator = BigInteger.TEN.pow(value.scale());
        BigInteger tmpGreatestCommonDivisor = unscaled.gcd(tmpDenominator);
        numerator = unscaled.divide(tmpGreatestCommonDivisor);
        denominator = tmpDenominator.divide(tmpGreatestCommonDivisor);
    } else {
        numerator = value.toBigIntegerExact();
        denominator = BigInteger.ONE;
    }
    return new Rational(numerator, denominator, BigInteger.ONE, numerator, denominator);
}

From source file:net.pms.util.Rational.java

/**
 * Returns an instance with the given {@code numerator} and
 * {@code denominator}.//from www  .  j av  a2  s .com
 *
 * @param numerator the numerator.
 * @param denominator the denominator.
 * @return An instance that represents the value of {@code numerator}/
 *         {@code denominator}.
 */
@Nullable
public static Rational valueOf(@Nullable BigDecimal numerator, @Nullable BigDecimal denominator) {
    if (numerator == null || denominator == null) {
        return null;
    }
    if (numerator.signum() == 0 && denominator.signum() == 0) {
        return NaN;
    }
    if (denominator.signum() == 0) {
        return numerator.signum() > 0 ? POSITIVE_INFINITY : NEGATIVE_INFINITY;
    }
    if (numerator.signum() == 0) {
        return ZERO;
    }
    if (numerator.equals(denominator)) {
        return ONE;
    }
    if (denominator.signum() < 0) {
        numerator = numerator.negate();
        denominator = denominator.negate();
    }

    int scale = Math.max(numerator.scale(), denominator.scale());
    if (scale > 0) {
        numerator = numerator.scaleByPowerOfTen(scale);
        denominator = denominator.scaleByPowerOfTen(scale);
    }
    BigInteger biNumerator = numerator.toBigIntegerExact();
    BigInteger biDenominator = denominator.toBigIntegerExact();

    BigInteger reducedNumerator;
    BigInteger reducedDenominator;
    BigInteger greatestCommonDivisor = calculateGreatestCommonDivisor(biNumerator, biDenominator);
    if (BigInteger.ONE.equals(greatestCommonDivisor)) {
        reducedNumerator = biNumerator;
        reducedDenominator = biDenominator;
    } else {
        reducedNumerator = biNumerator.divide(greatestCommonDivisor);
        reducedDenominator = biDenominator.divide(greatestCommonDivisor);
    }
    return new Rational(biNumerator, biDenominator, greatestCommonDivisor, reducedNumerator,
            reducedDenominator);
}

From source file:hr.restart.util.chart.ChartXY.java

public CategoryDataset createDataSet() throws NullPointerException {

    final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    DataSet ds = getDataSet();//from  w w w .  ja  v  a 2 s. c o m
    SortDescriptor old = ds.getSort();
    String key = getAxisX();
    String value = jcb == null ? getAxisY() : colNamesY[jcb.getSelectedIndex()];

    ds.setSort(new SortDescriptor(new String[] { value }, true, true));

    StorageDataSet scoped = new StorageDataSet();
    scoped.setLocale(Locale.getDefault());
    scoped.setColumns(new Column[] { ds.getColumn(key).cloneColumn(), ds.getColumn(value).cloneColumn(), });
    scoped.open();

    BigDecimal ostali = new BigDecimal(0);

    int maxElements = getNumberOfElements();

    if (comboBoxQuantity != null)
        maxElements = new Integer(comboBoxQuantity.getSelectedItem().toString()).intValue();

    int i = 0;
    for (ds.first(); ds.inBounds(); ds.next()) {
        i++;
        if (i > maxElements) {
            try {

                ostali = ostali.add(ds.getBigDecimal(value));
            } catch (Exception e) {
                System.out.println("(ChartXY) : method --> dataSetToMap : " + e);
                break;
            }
        } else {
            scoped.insertRow(false);

            scoped.setString(key, ds.getString(key));
            scoped.setBigDecimal(value, ds.getBigDecimal(value));
        }
    }

    if (ostali.signum() > 0) {
        scoped.insertRow(false);

        scoped.setString(key, "OSTALI");
        scoped.setBigDecimal(value, ostali);
    }
    scoped.post();

    if (sortByValue())
        scoped.setSort(ds.getSort());
    else
        scoped.setSort(new SortDescriptor(new String[] { key }));

    ds.setSort(old);

    for (scoped.first(); scoped.inBounds(); scoped.next())
        dataset.addValue(scoped.getBigDecimal(value).doubleValue(), "", scoped.getString(key));
    return dataset;
    //return initMapTest();
}

From source file:org.openbravo.advpaymentmngt.actionHandler.AddPaymentActionHandler.java

private void addSelectedPSDs(FIN_Payment payment, JSONObject jsonparams, List<String> pdToRemove)
        throws JSONException {
    JSONObject orderInvoiceGrid = jsonparams.getJSONObject("order_invoice");
    JSONArray selectedPSDs = orderInvoiceGrid.getJSONArray("_selection");
    for (int i = 0; i < selectedPSDs.length(); i++) {
        JSONObject psdRow = selectedPSDs.getJSONObject(i);
        String strPSDIds = psdRow.getString("id");
        String strPaidAmount = psdRow.getString("amount");
        BigDecimal paidAmount = new BigDecimal(strPaidAmount);

        boolean isWriteOff = psdRow.getBoolean("writeoff");
        // psdIds can be grouped
        String[] psdIds = strPSDIds.replaceAll(" ", "").split(",");
        List<FIN_PaymentScheduleDetail> psds = getOrderedPaymentScheduleDetails(psdIds);
        BigDecimal outstandingAmount = BigDecimal.ZERO;
        BigDecimal remainingAmount = paidAmount;
        for (FIN_PaymentScheduleDetail psd : psds) {
            BigDecimal assignAmount = BigDecimal.ZERO;

            if (psd.getPaymentDetails() != null) {
                // This schedule detail comes from an edited payment so outstanding amount needs to be
                // properly calculated
                List<FIN_PaymentScheduleDetail> outStandingPSDs = FIN_AddPayment.getOutstandingPSDs(psd);
                if (outStandingPSDs.size() > 0) {
                    outstandingAmount = psd.getAmount().add(outStandingPSDs.get(0).getAmount());
                } else {
                    outstandingAmount = psd.getAmount();
                }/* ww w .  j  a  v  a2 s. c o m*/
                pdToRemove.remove(psd.getPaymentDetails().getId());
            } else {
                outstandingAmount = psd.getAmount();
            }
            // Manage negative amounts
            if ((remainingAmount.signum() > 0 && remainingAmount.compareTo(outstandingAmount) >= 0)
                    || ((remainingAmount.signum() < 0 && outstandingAmount.signum() < 0)
                            && (remainingAmount.compareTo(outstandingAmount) <= 0))) {
                assignAmount = outstandingAmount;
                remainingAmount = remainingAmount.subtract(outstandingAmount);
            } else {
                assignAmount = remainingAmount;
                remainingAmount = BigDecimal.ZERO;
            }
            FIN_AddPayment.updatePaymentDetail(psd, payment, assignAmount, isWriteOff);
        }
    }
}

From source file:eu.europa.ec.fisheries.uvms.rules.service.business.AbstractFact.java

private boolean isIntegerValue(BigDecimal bigDecimal) {
    if (bigDecimal == null) {
        return false;
    }//w w  w  .j  a v a  2s.co  m

    if (bigDecimal.signum() == 0 || bigDecimal.scale() <= 0 || bigDecimal.stripTrailingZeros().scale() <= 0) {
        return true;
    } else {
        return false;
    }

}

From source file:com.healthmarketscience.jackcess.impl.ColumnImpl.java

/**
 * Writes a numeric value./*from w  w  w . j  a v a  2 s.  co m*/
 */
private void writeNumericValue(ByteBuffer buffer, Object value) throws IOException {
    Object inValue = value;
    try {
        BigDecimal decVal = toBigDecimal(value);
        inValue = decVal;

        int signum = decVal.signum();
        if (signum < 0) {
            decVal = decVal.negate();
        }

        // write sign byte
        buffer.put((signum < 0) ? NUMERIC_NEGATIVE_BYTE : 0);

        // adjust scale according to this column type (will cause the an
        // ArithmeticException if number has too many decimal places)
        decVal = decVal.setScale(getScale());

        // check precision
        if (decVal.precision() > getPrecision()) {
            throw new IOException(
                    "Numeric value is too big for specified precision " + getPrecision() + ": " + decVal);
        }

        // convert to unscaled BigInteger, big-endian bytes
        byte[] intValBytes = toUnscaledByteArray(decVal, getType().getFixedSize() - 1);
        if (buffer.order() != ByteOrder.BIG_ENDIAN) {
            fixNumericByteOrder(intValBytes);
        }
        buffer.put(intValBytes);
    } catch (ArithmeticException e) {
        throw (IOException) new IOException("Numeric value '" + inValue + "' out of range").initCause(e);
    }
}

From source file:org.egov.wtms.application.service.WaterConnectionDetailsService.java

public BigDecimal getTotalDemandTillCurrentFinYear(WaterConnectionDetails waterConnectionDetails) {
    EgDemand currentDemand = waterTaxUtils.getCurrentDemand(waterConnectionDetails).getDemand();
    BigDecimal balance = ZERO;
    if (currentDemand != null) {
        List<Object> instVsAmt = connectionDemandService
                .getDmdCollAmtInstallmentWiseUptoCurrentFinYear(currentDemand);
        balance = getTotalBalance(instVsAmt);
    }//from   w  w  w.  j  av  a 2s. co m
    if (balance.signum() < 0)
        balance = ZERO;
    return balance;
}

From source file:org.egov.wtms.application.service.WaterConnectionDetailsService.java

public BigDecimal getArrearsDemand(WaterConnectionDetails waterConnectionDetails) {
    EgDemand currentDemand = waterTaxUtils.getCurrentDemand(waterConnectionDetails).getDemand();
    BigDecimal balance = ZERO;
    if (currentDemand != null) {
        List<Object> instVsAmt = connectionDemandService
                .getDmdCollAmtInstallmentWiseUptoPreviousFinYear(currentDemand);
        balance = getTotalBalance(instVsAmt);
    }//from  w ww .ja va  2s . com
    if (balance.signum() < 0)
        balance = ZERO;
    return balance;
}

From source file:org.totschnig.myexpenses.activity.ExpenseEdit.java

protected void fillAmount(BigDecimal amount) {
    int signum = amount.signum();
    switch (signum) {
    case -1:/*from w  w w. j  a va 2 s  .com*/
        amount = amount.abs();
        break;
    case 1:
        mType = INCOME;
    }
    if (!mNewInstance) {
        mAmountText.setAmount(amount);
    }
    mAmountText.requestFocus();
    mAmountText.selectAll();
}

From source file:org.egov.wtms.application.service.WaterConnectionDetailsService.java

public BigDecimal getTotalAmountTillCurrentFinYear(WaterConnectionDetails waterConnectionDetails) {
    EgDemand currentDemand = waterTaxUtils.getCurrentDemand(waterConnectionDetails).getDemand();
    BigDecimal balance = ZERO;
    if (currentDemand != null) {
        List<Object> instVsAmt = connectionDemandService
                .getDmdCollAmtInstallmentWiseUptoCurrentFinYear(currentDemand);
        for (Object object : instVsAmt) {
            Object[] ddObject = (Object[]) object;
            BigDecimal dmdAmt = new BigDecimal((Double) ddObject[2]);
            BigDecimal collAmt = ZERO;
            if (ddObject[2] != null)
                collAmt = new BigDecimal((Double) ddObject[3]);
            balance = balance.add(dmdAmt.subtract(collAmt));
        }/*from   w w  w.j av a 2  s. co  m*/
    }
    if (balance.signum() < 0)
        balance = ZERO;
    return balance;
}