Example usage for java.math BigDecimal abs

List of usage examples for java.math BigDecimal abs

Introduction

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

Prototype

public BigDecimal abs() 

Source Link

Document

Returns a BigDecimal whose value is the absolute value of this BigDecimal , and whose scale is this.scale() .

Usage

From source file:Main.java

public static void main(String[] args) {

    // assign value to bg1
    BigDecimal bg1 = new BigDecimal("-40");

    // value before applying abs
    System.out.println("Value is " + bg1);

    // assign absolute value of bg1 to bg2
    BigDecimal bg2 = bg1.abs();

    // print bg2 value
    System.out.println("Absolute value is " + bg2);
}

From source file:MainClass.java

public static void main(String argv[]) {
    BigDecimal first = new BigDecimal("3419229223372036854775807.23343");
    BigDecimal second = new BigDecimal("2.0");
    System.out.println(first.add(second));
    System.out.println(first.subtract(second));
    System.out.println(first.divide(second));
    System.out.println(first.equals(second));
    System.out.println(first.abs());
    System.out.println(first.max(second));
    System.out.println(first.min(second));
    System.out.println(first.remainder(second));
}

From source file:com.nearinfinity.honeycomb.hbase.bulkload.FieldParser.java

private static ByteBuffer extractDecimal(String val, int precision, int right_scale) {
    int left_scale = precision - 2;
    BigDecimal x = new BigDecimal(val);
    boolean is_negative = x.compareTo(BigDecimal.ZERO) == -1;
    x = x.abs();
    BigDecimal left = x.setScale(0, RoundingMode.DOWN);
    BigDecimal right = x.subtract(left).movePointRight(right_scale);
    int right_bytes_len = bytesFromDigits(right_scale);
    int left_bytes_len = bytesFromDigits(left_scale);
    byte[] left_bytes = left.toBigInteger().toByteArray();
    byte[] right_bytes = right.toBigInteger().toByteArray();
    // Bit twiddling is fun
    byte[] buff = new byte[left_bytes_len + right_bytes_len];

    System.arraycopy(left_bytes, 0, buff, left_bytes_len - left_bytes.length, left_bytes.length);
    System.arraycopy(right_bytes, 0, buff, right_bytes_len - right_bytes.length + left_bytes_len,
            right_bytes.length);//ww  w  .  j  a va2 s .  c  o m

    buff[0] ^= -128; // Flip first bit, 0x80
    if (is_negative) { // Flip all bits
        for (int i = 0; i < buff.length; i++) {
            buff[i] ^= -1; // 0xff
        }
    }
    return ByteBuffer.wrap(buff);
}

From source file:org.openmrs.module.openhmis.cashier.api.util.RoundingUtil.java

private static void updateRoundingItem(Bill bill, BigDecimal difference, BillLineItem roundingLineItem) {
    roundingLineItem.setPrice(difference.abs());
    roundingLineItem.setQuantity(difference.compareTo(BigDecimal.ZERO) > 0 ? -1 : 1);

    bill.removeLineItem(roundingLineItem);
    bill.addLineItem(roundingLineItem);//  w  w w.j av a 2 s.c o m
}

From source file:org.openmrs.module.openhmis.cashier.api.util.RoundingUtil.java

/**
 * Add a rounding line item to a bill if necessary
 * @param bill/* www.ja  v  a  2s.  c o  m*/
 * @should handle a rounding line item (add/update/delete with the appropriate value)
 * @should not modify a bill that needs no rounding
 * @should round bills with a non zero amount correctly for MID
 * @should round bills with a non zero amount correctly for CEILING
 * @should round bills with a non zero amount correctly for FLOOR
 */
public static void handleRoundingLineItem(Bill bill) {
    ICashierOptionsService cashOptService = Context.getService(ICashierOptionsService.class);
    CashierOptions options = cashOptService.getOptions();
    if (options.getRoundToNearest().equals(BigDecimal.ZERO)) {
        return;
    }

    if (options.getRoundingItemUuid() == null) {
        throw new APIException(
                "No rounding item specified in options. This must be set in order to use rounding for bill totals.");
    }

    // Get rounding item
    IItemDataService itemService = Context.getService(IItemDataService.class);
    Item roundingItem = itemService.getByUuid(options.getRoundingItemUuid());

    BillLineItem roundingLineItem = findRoundingLineItem(bill, roundingItem);

    BigDecimal difference = calculateRoundingValue(bill, options, roundingLineItem);

    if (difference.equals(BigDecimal.ZERO) && roundingLineItem != null) {
        bill.removeLineItem(roundingLineItem);
    } else if (!difference.equals(BigDecimal.ZERO) && roundingLineItem == null) {
        // Create line item for rounding item and the required amount
        bill.addLineItem(roundingItem, difference.abs(), "",
                difference.compareTo(BigDecimal.ZERO) > 0 ? -1 : 1);
    } else if (!difference.equals(BigDecimal.ZERO)) {
        updateRoundingItem(bill, difference, roundingLineItem);
    }
    bill.recalculateLineItemOrder();
}

From source file:org.cirdles.geoapp.LatLongToUTM.java

public static UTM convert(BigDecimal latitude, BigDecimal longitude, String datumName) {

    DatumEnum datumEnum = DatumEnum.valueOf(datumName);
    BigDecimal flattening3D = new BigDecimal(datumEnum.getFlattening3D());
    BigDecimal meridianRadius = new BigDecimal(datumEnum.getMeridianRadius());
    BigDecimal eccentricity = new BigDecimal(datumEnum.getEccentricity());

    BigDecimal latitudeRadians = latitude.abs().multiply(new BigDecimal(Math.PI)).divide(new BigDecimal(180.0),
            precision, RoundingMode.HALF_UP);

    //System.out.println("Latitude Radians: " + latitudeRadians);

    int zoneNumber = calcZoneNumber(longitude);

    BigDecimal zoneCentralMeridian = calcZoneCentralMeridian(zoneNumber);

    BigDecimal changeInLongitudeDegree = (longitude.subtract(zoneCentralMeridian)).abs().setScale(precision,
            RoundingMode.HALF_UP);
    //System.out.println("Change in Long Degree: " + changeInLongitudeDegree);

    BigDecimal changeInLongitudeRadians = (changeInLongitudeDegree.multiply(new BigDecimal(Math.PI)))
            .divide(new BigDecimal(180), precision, RoundingMode.HALF_UP);
    //System.out.println("Change In Longitude Radians: " + changeInLongitudeRadians);

    BigDecimal conformalLatitude = calcConformalLatitude(eccentricity, latitudeRadians).setScale(precision,
            RoundingMode.HALF_UP);
    //System.out.println("Conformal Latitude: " + conformalLatitude);

    BigDecimal tauPrime = (new BigDecimal(Math.tan(conformalLatitude.doubleValue()))).setScale(precision,
            RoundingMode.HALF_UP);
    //System.out.println("Tau Prime: " + tauPrime);

    BigDecimal xiPrimeNorth = calcXiPrimeNorth(changeInLongitudeRadians, tauPrime).setScale(precision,
            RoundingMode.HALF_UP);
    //System.out.println("xi Prime North: " + xiPrimeNorth);

    BigDecimal etaPrimeEast = calcEtaPrimeEast(changeInLongitudeRadians, tauPrime).setScale(precision,
            RoundingMode.HALF_UP);
    //System.out.println("Eta Prime East: " + etaPrimeEast);

    BigDecimal[] alphaSeries = { KrugerSeries.alpha1(flattening3D).setScale(precision, RoundingMode.HALF_UP),
            KrugerSeries.alpha2(flattening3D.setScale(precision, RoundingMode.HALF_UP)),
            KrugerSeries.alpha3(flattening3D).setScale(precision, RoundingMode.HALF_UP),
            KrugerSeries.alpha4(flattening3D).setScale(precision, RoundingMode.HALF_UP),
            KrugerSeries.alpha5(flattening3D).setScale(precision, RoundingMode.HALF_UP),
            KrugerSeries.alpha6(flattening3D).setScale(precision, RoundingMode.HALF_UP),
            KrugerSeries.alpha7(flattening3D).setScale(precision, RoundingMode.HALF_UP) };

    BigDecimal xiNorth = calcXiNorth(xiPrimeNorth, etaPrimeEast, alphaSeries).setScale(precision,
            RoundingMode.HALF_UP);
    //System.out.println("xi North: " + xiNorth);

    BigDecimal etaEast = calcEtaEast(xiPrimeNorth, etaPrimeEast, alphaSeries).setScale(precision,
            RoundingMode.HALF_UP);
    //System.out.println("Eta East: " + etaEast);

    BigDecimal easting = calcEasting(meridianRadius, etaEast, longitude, zoneCentralMeridian)
            .setScale(precision, RoundingMode.HALF_UP);
    BigDecimal northing = calcNorthing(meridianRadius, xiNorth, latitude).setScale(precision,
            RoundingMode.HALF_UP);

    char zoneLetter = calcZoneLetter(latitude);
    char hemisphere = calcHemisphere(latitude);

    return new UTM(easting, northing, hemisphere, zoneNumber, zoneLetter);

}

From source file:org.cirdles.ambapo.LatLongToUTM.java

/**
 * Converts BigDecimal latitude longitude to UTM 
 * /*from  www .  j a v a2  s  .co  m*/
 * @param latitude
 * @param longitude
 * @param datumName
 * @return UTM
 * @throws java.lang.Exception
 * 
 * 
 */
public static UTM convert(BigDecimal latitude, BigDecimal longitude, String datumName) throws Exception {

    Datum datum = Datum.valueOf(datumName);

    BigDecimal meridianRadius = new BigDecimal(datum.getMeridianRadius());
    BigDecimal eccentricity = new BigDecimal(datum.getEccentricity());

    BigDecimal latitudeRadians = latitude.abs().multiply(new BigDecimal(Math.PI)).divide(new BigDecimal(180.0),
            PRECISION, RoundingMode.HALF_UP);

    int zoneNumber = calcZoneNumber(longitude);

    BigDecimal zoneCentralMeridian = calcZoneCentralMeridian(zoneNumber);

    BigDecimal changeInLongitudeDegree = (longitude.subtract(zoneCentralMeridian)).abs().setScale(PRECISION,
            RoundingMode.HALF_UP);

    BigDecimal changeInLongitudeRadians = (changeInLongitudeDegree.multiply(new BigDecimal(Math.PI)))
            .divide(new BigDecimal(180), PRECISION, RoundingMode.HALF_UP);

    BigDecimal conformalLatitude = calcConformalLatitude(eccentricity, latitudeRadians).setScale(PRECISION,
            RoundingMode.HALF_UP);

    BigDecimal tauPrime = (new BigDecimal(Math.tan(conformalLatitude.doubleValue()))).setScale(PRECISION,
            RoundingMode.HALF_UP);

    BigDecimal xiPrimeNorth = calcXiPrimeNorth(changeInLongitudeRadians, tauPrime).setScale(PRECISION,
            RoundingMode.HALF_UP);

    BigDecimal etaPrimeEast = calcEtaPrimeEast(changeInLongitudeRadians, tauPrime).setScale(PRECISION,
            RoundingMode.HALF_UP);

    double[] alphaSeries = datum.getAlphaSeries();

    BigDecimal xiNorth = calcXiNorth(xiPrimeNorth, etaPrimeEast, alphaSeries).setScale(PRECISION,
            RoundingMode.HALF_UP);

    BigDecimal etaEast = calcEtaEast(xiPrimeNorth, etaPrimeEast, alphaSeries).setScale(PRECISION,
            RoundingMode.HALF_UP);

    BigDecimal easting = calcEasting(meridianRadius, etaEast, longitude, zoneCentralMeridian)
            .setScale(PRECISION, RoundingMode.HALF_UP);
    BigDecimal northing = calcNorthing(meridianRadius, xiNorth, latitude).setScale(PRECISION,
            RoundingMode.HALF_UP);

    char zoneLetter = calcZoneLetter(latitude);
    char hemisphere = calcHemisphere(latitude);

    if (easting.doubleValue() > UTM.MAX_EASTING)
        easting = new BigDecimal(UTM.MAX_EASTING);
    if (easting.doubleValue() < UTM.MIN_EASTING)
        easting = new BigDecimal(UTM.MIN_EASTING);

    if (northing.doubleValue() > UTM.MAX_NORTHING)
        northing = new BigDecimal(UTM.MAX_NORTHING);
    if (northing.doubleValue() < UTM.MIN_NORTHING)
        northing = new BigDecimal(UTM.MIN_NORTHING);

    UTM utm = new UTM(easting.setScale(SCALE, RoundingMode.HALF_UP),
            northing.setScale(SCALE, RoundingMode.HALF_UP), hemisphere, zoneNumber, zoneLetter);

    return utm;
}

From source file:org.openbravo.advpaymentmngt.utility.APRM_MatchingUtility.java

/**
 * Merges given bank statement line with other bank statement lines with the same line number and
 * not matched with any transaction.//w w  w . j  av a  2 s  .  c om
 * 
 * @param bsline
 *          Bank Statement Line.
 */
private static void mergeBankStatementLine(FIN_BankStatementLine bsline) {
    BigDecimal totalCredit = bsline.getCramount();
    BigDecimal totalDebit = bsline.getDramount();
    FIN_BankStatement bs = bsline.getBankStatement();
    OBCriteria<FIN_BankStatementLine> obc = OBDal.getInstance().createCriteria(FIN_BankStatementLine.class);
    obc.add(Restrictions.eq(FIN_BankStatementLine.PROPERTY_BANKSTATEMENT, bsline.getBankStatement()));
    obc.add(Restrictions.eq(FIN_BankStatementLine.PROPERTY_LINENO, bsline.getLineNo()));
    obc.add(Restrictions.ne(FIN_BankStatementLine.PROPERTY_ID, bsline.getId()));
    obc.add(Restrictions.isNull(FIN_BankStatementLine.PROPERTY_FINANCIALACCOUNTTRANSACTION));
    // Following list should contain just 2 elements maximum
    final List<FIN_BankStatementLine> splitLines = obc.list();

    if (!splitLines.isEmpty()) {
        // bs is set a false, because it is processed bs cannot be edited
        bs.setProcessed(false);
        OBDal.getInstance().save(bs);
        OBDal.getInstance().flush();

        for (final FIN_BankStatementLine bsl : splitLines) {
            totalCredit = totalCredit.add(bsl.getCramount());
            totalDebit = totalDebit.add(bsl.getDramount());
            OBDal.getInstance().remove(bsl);
        }

        if (totalCredit.compareTo(BigDecimal.ZERO) != 0 && totalDebit.compareTo(BigDecimal.ZERO) != 0) {
            BigDecimal total = totalCredit.subtract(totalDebit);
            if (total.compareTo(BigDecimal.ZERO) == -1) {
                bsline.setCramount(BigDecimal.ZERO);
                bsline.setDramount(total.abs());
            } else {
                bsline.setCramount(total);
                bsline.setDramount(BigDecimal.ZERO);
            }
        } else {
            bsline.setCramount(totalCredit);
            bsline.setDramount(totalDebit);
        }
        // Required to persist current bank statement line merges so that it is not rollbacked
        // afterwards because of a future error
        OBDal.getInstance().save(bsline);
        OBDal.getInstance().flush();

        // bs is set a true, because it was the status at the beginning of this function
        bs.setProcessed(true);
        OBDal.getInstance().save(bs);
        OBDal.getInstance().flush();
    }

}

From source file:edu.ku.brc.specify.config.LatLonConverter.java

public static String convertToSignedDDDDDD(final BigDecimal dd, final int decimalLen,
        final DEGREES_FORMAT degFmt) {
    String sign = "";
    if (dd.compareTo(dd.abs()) < 0) {
        sign = "-";
    }// ww  w .ja  va  2 s  .  c o m

    String convertedAbs = convertToDDDDDD(dd, degFmt, DIRECTION.None, decimalLen);
    return sign + convertedAbs;
}

From source file:edu.ku.brc.specify.config.LatLonConverter.java

/**
 * @param bd//from   w ww  .j a va 2  s.c o m
 * @param decimalLen
 * @param degFmt
 * @return
 */
public static String convertToSignedDDMMSS(final BigDecimal bd, final int decimalLen,
        final DEGREES_FORMAT degFmt) {
    String sign = "";
    if (bd.compareTo(bd.abs()) < 0) {
        sign = "-";
    }

    String convertedAbs = convertToDDMMSS(bd, degFmt, DIRECTION.None, decimalLen);
    return sign + convertedAbs;
}