Example usage for java.math BigDecimal divide

List of usage examples for java.math BigDecimal divide

Introduction

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

Prototype

public BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode) 

Source Link

Document

Returns a BigDecimal whose value is (this / divisor) , and whose scale is as specified.

Usage

From source file:org.silverpeas.core.util.time.Duration.java

public static BigDecimal convertTo(BigDecimal value, final TimeUnit from, final TimeUnit to) {
    int fromIndex = orderedUnits.indexOf(from);
    int toIndex = orderedUnits.indexOf(to);
    final int offsetIndex = fromIndex - toIndex;
    if (offsetIndex > 0) {
        return value.multiply(d(from).getMultiplier(d(to))).setScale(15, BigDecimal.ROUND_DOWN);
    } else if (offsetIndex < 0) {
        return value.divide(d(from).getMultiplier(d(to)), 15, BigDecimal.ROUND_DOWN);
    }// w w w.j ava  2 s .c  om
    return value;
}

From source file:universal.Calc.java

public static BigDecimal sqrt(BigDecimal A, final int SCALE) {
    BigDecimal x0 = new BigDecimal("0");
    BigDecimal x1 = new BigDecimal(Math.sqrt(A.doubleValue()));
    while (!x0.equals(x1)) {
        x0 = x1;//from   w  ww.j a  v a  2s  .  c  om
        x1 = A.divide(x0, SCALE, RoundingMode.HALF_UP);
        x1 = x1.add(x0);
        x1 = x1.divide(new BigDecimal(2), SCALE, RoundingMode.HALF_UP);
    }
    return x1;
}

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

/**
 * Calculates the xi-north which refers to the north-south direction of the UTM.
 * @param hemisphere/*from  w w  w . j a va2  s .  co  m*/
 * @param meridianRadius
 * @param northing
 * @return xi north
 */
private static BigDecimal calcXiNorth(char hemisphere, BigDecimal meridianRadius, BigDecimal northing) {

    BigDecimal xiNorth;

    if (hemisphere == 'N') {

        BigDecimal divideByThis = SCALE_FACTOR.multiply(meridianRadius).setScale(PRECISION,
                RoundingMode.HALF_UP);

        xiNorth = northing.divide(divideByThis, PRECISION, RoundingMode.HALF_UP);

    }

    else {

        BigDecimal numerator = (new BigDecimal(10000000)).subtract(northing);
        BigDecimal denominator = SCALE_FACTOR.multiply(meridianRadius);

        xiNorth = numerator.divide(denominator, PRECISION, RoundingMode.HALF_UP);

    }

    return xiNorth;

}

From source file:org.sakaiproject.profile2.job.KudosJob.java

/**
 * Gets the score out of ten as an int, and rounded up
 * @param score      score for user//from   w  ww.  ja v a2s.c om
 * @param total      total possible score
 * @return
 */
private static int getScoreOutOfTen(BigDecimal score, BigDecimal total) {
    return score.divide(total, 1, RoundingMode.HALF_UP).multiply(new BigDecimal("10")).intValue();
}

From source file:org.silverpeas.core.util.time.TimeData.java

/**
 * Converting a value//from  w  w  w  .  j  a v a  2 s .c  o m
 * @param value
 * @param from
 * @param to
 * @return
 */
public static BigDecimal convertTo(BigDecimal value, final TimeUnit from, final TimeUnit to) {
    int fromIndex = orderedUnits.indexOf(from);
    int toIndex = orderedUnits.indexOf(to);
    final int offsetIndex = fromIndex - toIndex;
    if (offsetIndex > 0) {
        return value.multiply(from.getMultiplier(to)).setScale(15, BigDecimal.ROUND_DOWN);
    } else if (offsetIndex < 0) {
        return value.divide(from.getMultiplier(to), 15, BigDecimal.ROUND_DOWN);
    }
    return value;
}

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

/**
 * /*  www .  ja  va 2s.c om*/
 * @param originalTau
 * @param sigma
 * @param eccentricity
 * @param hemisphere
 * @return latitude
 */
private static BigDecimal calcLatitude(BigDecimal originalTau, BigDecimal sigma, BigDecimal eccentricity,
        char hemisphere) {

    BigDecimal funcOfTau = functionOfTau(originalTau, sigma, originalTau).setScale(PRECISION,
            RoundingMode.HALF_UP);

    BigDecimal changeInTau = changeInTau(eccentricity, originalTau, sigma);

    BigDecimal newTau = originalTau.subtract(funcOfTau.divide(changeInTau, PRECISION, RoundingMode.HALF_UP));

    BigDecimal latitude = (new BigDecimal(Math.atan(newTau.doubleValue())))
            .multiply(new BigDecimal(180.0 / Math.PI));

    if (hemisphere == 'S')
        latitude = latitude.multiply(new BigDecimal(-1));

    return latitude;

}

From source file:com.wineaccess.winerypermit.WineryPermitHelper.java

/**
 * @param startDate/*from   w ww .  j av  a2s . c o m*/
 * @param endDate
 * @return
 */
public static final Integer getMonthsDifference(Date startDate, Date endDate) {

    Calendar startCalendar = new GregorianCalendar();
    startCalendar.setTime(startDate);
    Calendar endCalendar = new GregorianCalendar();
    endCalendar.setTime(endDate);

    int diffYear = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
    BigDecimal temp = new BigDecimal(endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH));
    temp = temp.divide(new BigDecimal(12), 2, RoundingMode.HALF_UP);
    int monthDiff = diffYear + temp.setScale(0, RoundingMode.HALF_UP).intValue();

    return monthDiff * 12;
}

From source file:Main.java

/**
 * Compute e^x to a given scale by the Taylor series.
 * @param x the value of x/*from   ww  w .  j  a  v  a  2s. co  m*/
 * @param scale the desired scale of the result
 * @return the result value
 */
private static BigDecimal expTaylor(BigDecimal x, int scale) {
    BigDecimal factorial = BigDecimal.valueOf(1);
    BigDecimal xPower = x;
    BigDecimal sumPrev;

    // 1 + x
    BigDecimal sum = x.add(BigDecimal.valueOf(1));

    // Loop until the sums converge
    // (two successive sums are equal after rounding).
    int i = 2;
    do {
        // x^i
        xPower = xPower.multiply(x).setScale(scale, BigDecimal.ROUND_HALF_EVEN);

        // i!
        factorial = factorial.multiply(BigDecimal.valueOf(i));

        // x^i/i!
        BigDecimal term = xPower.divide(factorial, scale, BigDecimal.ROUND_HALF_EVEN);

        // sum = sum + x^i/i!
        sumPrev = sum;
        sum = sum.add(term);

        ++i;
        Thread.yield();
    } while (sum.compareTo(sumPrev) != 0);

    return sum;
}

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

private static int calcZoneNumber(BigDecimal longitude) {
    int zoneNumber;
    BigDecimal six = new BigDecimal(6);

    if (longitude.signum() < 0) {

        BigDecimal oneEighty = new BigDecimal(180);
        zoneNumber = ((oneEighty.add(longitude)).divide(six, precision, RoundingMode.HALF_UP)).intValue() + 1;
    }//from   ww w. jav a 2  s .  co  m

    else {

        BigDecimal thirtyOne = new BigDecimal(31);
        zoneNumber = ((longitude.divide(six, precision, RoundingMode.HALF_UP)).abs().add(thirtyOne)).intValue();
    }

    return zoneNumber;

}

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

/**
 * //ww w . j  a  v a2s.  c o m
 * @param xiPrime
 * @param etaPrime
 * @return tau prime
 */
private static BigDecimal calcTauPrime(BigDecimal xiPrime, BigDecimal etaPrime) {

    double xiPrimeDouble = xiPrime.doubleValue();
    double etaPrimeDouble = etaPrime.doubleValue();

    BigDecimal sinOfXiPrime = new BigDecimal(Math.sin(xiPrimeDouble));
    BigDecimal cosOfXiPrime = new BigDecimal(Math.cos(xiPrimeDouble));
    BigDecimal sinhOfEtaPrime = new BigDecimal(Math.sinh(etaPrimeDouble));

    BigDecimal squareRoot = new BigDecimal(
            Math.sqrt(sinhOfEtaPrime.pow(2).add(cosOfXiPrime.pow(2)).doubleValue()));

    BigDecimal tauPrime = sinOfXiPrime.divide(squareRoot, PRECISION, RoundingMode.HALF_UP);

    return tauPrime;
}