Example usage for java.math BigDecimal setScale

List of usage examples for java.math BigDecimal setScale

Introduction

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

Prototype

@Deprecated(since = "9")
public BigDecimal setScale(int newScale, int roundingMode) 

Source Link

Document

Returns a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal 's unscaled value by the appropriate power of ten to maintain its overall value.

Usage

From source file:org.kuali.kpme.core.util.TKUtils.java

public static int convertMillisToWholeDays(long millis) {
    BigDecimal days = convertMillisToDays(millis);
    return Integer.parseInt(days.setScale(0, BigDecimal.ROUND_UP).toString());
}

From source file:com.prowidesoftware.swift.utils.SwiftFormatUtils.java

/**
 * Return the number of decimals for the given number, which can be <code>null</code>, in which case this method returns zero.
 * //from   w w  w .j a v a2s.  co  m
 * @return the number of decimal in the number or zero if there are none or the amount is <code>null</code>
 * @since 7.8
 */
public static int decimalsInAmount(final BigDecimal amount) {
    if (amount != null) {
        BigDecimal d = new BigDecimal(amount.toString());
        BigDecimal result = d.subtract(d.setScale(0, RoundingMode.FLOOR)).movePointRight(d.scale());
        if (result.intValue() != 0) {
            return result.toString().length();
        }
    }
    return 0;
}

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

/**
 * @param startDate/*from w  w w . j  a  v a2s .com*/
 * @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:ext.paycenter.PayService.java

/**
 * ?token/*from   w ww  . j  a v a  2  s.c  o m*/
 * 
 * @param token token
 * @param email ?
 * @param payment ????
 * @param serveduration ?
 * @param isIgnoreLocked ??  true
 * @return
 */
public static TransferResult transferByToken(String token, String email, BigDecimal payment, Long serveduration,
        boolean isIgnoreLocked) {
    if (StringUtils.isBlank(token) || StringUtils.isBlank(email) || null == payment || null == serveduration) {
        throw new IllegalArgumentException("??token = " + token + ", email = " + email
                + ", payment = " + payment + ", serveduration = " + serveduration);
    }

    String paymentString = payment.setScale(2, BigDecimal.ROUND_HALF_UP).toString();

    PCResult<Void> result = PCClient.transferax(token, email, paymentString, String.valueOf(serveduration),
            isIgnoreLocked);
    if (result.balanceNotEnough()) {
        return TransferResult.BALANCE_NOT_ENOUGH;
    } else if (result.isSuccess()) {
        return TransferResult.SUCCESS;
    } else if (result.noMatchData()) {
        return TransferResult.INVALID_TOKEN;
    } else if (result.accountLocked()) {
        return TransferResult.ACCOUNT_LOCKED;
    } else {
        LOGGER.error("call transferax error result = " + result);
    }

    return TransferResult.UNKNOWN_ERROR;
}

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

/**
 * Converts a UTM into a Coordinate of lat,long with a specific datum.
 * @param utm// w w  w  .ja v  a  2s .  com
 * @param datum
 * @return Coordinate
 * @throws Exception 
 */
public static Coordinate convert(UTM utm, String datum) throws Exception {

    Datum datumInformation = Datum.valueOf(datum);

    double[] betaSeries = datumInformation.getBetaSeries();

    char hemisphere = utm.getHemisphere();

    double zoneCentralMeridian = utm.getZoneNumber() * 6 - 183;

    BigDecimal meridianRadius = new BigDecimal(datumInformation.getMeridianRadius());

    BigDecimal northing = utm.getNorthing();

    BigDecimal easting = utm.getEasting();

    BigDecimal xiNorth = calcXiNorth(hemisphere, meridianRadius, northing);

    BigDecimal etaEast = calcEtaEast(easting, meridianRadius);

    BigDecimal xiPrime = calcXiPrime(xiNorth, etaEast, betaSeries);

    BigDecimal etaPrime = calcEtaPrime(xiNorth, etaEast, betaSeries);

    BigDecimal tauPrime = calcTauPrime(xiPrime, etaPrime);

    BigDecimal eccentricity = new BigDecimal(datumInformation.getEccentricity());

    BigDecimal sigma = calcSigma(eccentricity, tauPrime);

    BigDecimal longitude = calcLongitude(zoneCentralMeridian, etaPrime, xiPrime);

    if (longitude.doubleValue() > Coordinate.MAX_LONGITUDE)
        longitude = new BigDecimal(Coordinate.MAX_LONGITUDE);

    if (longitude.doubleValue() < Coordinate.MIN_LONGITUDE)
        longitude = new BigDecimal(Coordinate.MIN_LONGITUDE);

    BigDecimal latitude = calcLatitude(tauPrime, sigma, eccentricity, hemisphere);

    if (latitude.doubleValue() > Coordinate.MAX_LATITUDE)
        latitude = new BigDecimal(Coordinate.MAX_LATITUDE);
    if (latitude.doubleValue() < Coordinate.MIN_LATITUDE)
        latitude = new BigDecimal(Coordinate.MIN_LATITUDE);

    Coordinate latAndLong = new Coordinate(latitude.setScale(SCALE, BigDecimal.ROUND_HALF_UP),
            longitude.setScale(SCALE, BigDecimal.ROUND_HALF_UP), datum);

    return latAndLong;

}

From source file:ext.paycenter.PayService.java

/**
 * ?email/*from ww w .java2s  .c  om*/
 * 
 * @param email 
 * @param toEmail ?
 * @param payment ????
 * @param serveduration ?
 * @param isIgnoreLocked ??    true
 * @return
 */
public static TransferResult transferByEmail(String email, String toEmail, BigDecimal payment,
        Long serveduration, boolean isIgnoreLocked) {
    if (StringUtils.isBlank(email) || StringUtils.isBlank(toEmail) || null == payment
            || null == serveduration) {
        throw new IllegalArgumentException("??email = " + email + ", toEmail = " + toEmail
                + ", payment = " + payment + ", serveduration = " + serveduration);
    }

    String paymentString = payment.setScale(2, BigDecimal.ROUND_HALF_UP).toString();

    PCResult<Void> result = PCClient.transferByEmail(email, toEmail, paymentString,
            String.valueOf(serveduration), isIgnoreLocked);
    if (result.balanceNotEnough()) {
        return TransferResult.BALANCE_NOT_ENOUGH;
    } else if (result.isSuccess()) {
        return TransferResult.SUCCESS;
    } else if (result.noMatchData()) {
        return TransferResult.INVALID_TOKEN;
    } else if (result.accountLocked()) {
        return TransferResult.ACCOUNT_LOCKED;
    } else {
        LOGGER.error("call transferByEmail error result = " + result);
    }

    return TransferResult.UNKNOWN_ERROR;
}

From source file:net.tradelib.misc.StrategyText.java

static private String formatOrderPrice(BigDecimal bd) {
    bd = bd.setScale(7, RoundingMode.HALF_UP).stripTrailingZeros();
    int scale = bd.scale() <= 2 ? 2 : bd.scale();
    return String.format("%." + Integer.toString(scale) + "f", bd);
}

From source file:net.tradelib.misc.StrategyText.java

static private String formatBigDecimal(BigDecimal bd, int minPrecision, int maxPrecision) {
    bd = bd.setScale(maxPrecision, RoundingMode.HALF_UP).stripTrailingZeros();
    int scale = bd.scale() <= minPrecision ? minPrecision : bd.scale();
    return String.format("%,." + Integer.toString(scale) + "f", bd);
}

From source file:net.tradelib.misc.StrategyText.java

static private String formatDouble(double dd, int minPrecision, int maxPrecision) {
    BigDecimal bd = new BigDecimal(dd, MathContext.DECIMAL128);
    bd = bd.setScale(maxPrecision, RoundingMode.HALF_UP);
    return formatBigDecimal(bd);
}

From source file:org.fcrepo.client.test.PerformanceTests.java

private static double round(double d) {
    int decimalPlace = 5;
    BigDecimal bd = new BigDecimal(Double.toString(d));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
    return bd.doubleValue();
}