List of usage examples for java.math BigDecimal divide
public BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode)
From source file:es.juntadeandalucia.panelGestion.negocio.utiles.Utils.java
public static String formatSize(Integer size) { String formattedFileSize = ""; if (size != null) { BigDecimal kb = BigDecimal.valueOf(1024); BigDecimal mb = BigDecimal.valueOf(1048576); BigDecimal fileSizeBD = BigDecimal.valueOf(size); if (size > 999999) { formattedFileSize = fileSizeBD.divide(mb, 2, RoundingMode.HALF_UP).toPlainString(); formattedFileSize += " MB"; } else if (size > 999) { formattedFileSize = fileSizeBD.divide(kb, 2, RoundingMode.HALF_UP).toPlainString(); formattedFileSize += " KB"; } else {// w w w . j a v a 2s .c o m formattedFileSize = fileSizeBD.toPlainString(); formattedFileSize += " B"; } } return formattedFileSize; }
From source file:Main.java
/** * Compute the integral root of x to a given scale, x >= 0. * Use Newton's algorithm./*w w w. j a v a 2 s.co m*/ * @param x the value of x * @param index the integral root value * @param scale the desired scale of the result * @return the result value */ public static BigDecimal intRoot(BigDecimal x, long index, int scale) { // Check that x >= 0. if (x.signum() < 0) { throw new IllegalArgumentException("x < 0"); } int sp1 = scale + 1; BigDecimal n = x; BigDecimal i = BigDecimal.valueOf(index); BigDecimal im1 = BigDecimal.valueOf(index - 1); BigDecimal tolerance = BigDecimal.valueOf(5).movePointLeft(sp1); BigDecimal xPrev; // The initial approximation is x/index. x = x.divide(i, scale, BigDecimal.ROUND_HALF_EVEN); // Loop until the approximations converge // (two successive approximations are equal after rounding). do { // x^(index-1) BigDecimal xToIm1 = intPower(x, index - 1, sp1); // x^index BigDecimal xToI = x.multiply(xToIm1).setScale(sp1, BigDecimal.ROUND_HALF_EVEN); // n + (index-1)*(x^index) BigDecimal numerator = n.add(im1.multiply(xToI)).setScale(sp1, BigDecimal.ROUND_HALF_EVEN); // (index*(x^(index-1)) BigDecimal denominator = i.multiply(xToIm1).setScale(sp1, BigDecimal.ROUND_HALF_EVEN); // x = (n + (index-1)*(x^index)) / (index*(x^(index-1))) xPrev = x; x = numerator.divide(denominator, sp1, BigDecimal.ROUND_DOWN); Thread.yield(); } while (x.subtract(xPrev).abs().compareTo(tolerance) > 0); return x; }
From source file:org.cirdles.ambapo.LatLongToUTM.java
/** * Returns the zone number that the longitude corresponds to on the UTM map * /*from ww w. ja va 2 s .c o m*/ * @param longitude * @return int zone number * * */ 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; } else { BigDecimal thirtyOne = new BigDecimal(31); zoneNumber = ((longitude.divide(six, PRECISION, RoundingMode.HALF_UP)).abs().add(thirtyOne)).intValue(); } if (zoneNumber > 60) zoneNumber = 60; if (zoneNumber < 1) zoneNumber = 1; return zoneNumber; }
From source file:com.vsthost.rnd.commons.math.ext.linear.DMatrixUtils.java
/** * Returns the DOWN rounded value of the given value for the given steps. * * @param value The original value to be rounded. * @param steps The steps./*from ww w.jav a 2s .c o m*/ * @return The DOWN rounded value of the given value for the given steps. */ public static BigDecimal roundDownTo(double value, double steps) { final BigDecimal bValue = BigDecimal.valueOf(value); final BigDecimal bSteps = BigDecimal.valueOf(steps); if (Objects.equals(bSteps, BigDecimal.ZERO)) { return bValue; } else { return bValue.divide(bSteps, 0, RoundingMode.FLOOR).multiply(bSteps); } }
From source file:com.vsthost.rnd.commons.math.ext.linear.DMatrixUtils.java
/** * Returns the UP rounded value of the given value for the given steps. * * @param value The original value to be rounded. * @param steps The steps.//from ww w.j a v a 2 s . co m * @return The UP rounded value of the given value for the given steps. */ public static BigDecimal roundUpTo(double value, double steps) { final BigDecimal bValue = BigDecimal.valueOf(value); final BigDecimal bSteps = BigDecimal.valueOf(steps); if (Objects.equals(bSteps, BigDecimal.ZERO)) { return bValue; } else { return bValue.divide(bSteps, 0, RoundingMode.CEILING).multiply(bSteps); } }
From source file:org.sparkcommerce.core.offer.service.discount.domain.PromotableOfferUtility.java
public static Money computeAdjustmentValue(Money currentPriceDetailValue, BigDecimal offerUnitValue, OfferHolder offerHolder, PromotionRounding rounding) { Offer offer = offerHolder.getOffer(); SparkCurrency currency = offerHolder.getCurrency(); OfferDiscountType discountType = offer.getDiscountType(); Money adjustmentValue;//from w ww .ja v a 2 s. co m if (currency != null) { adjustmentValue = new Money(currency); } else { adjustmentValue = new Money(); } if (OfferDiscountType.AMOUNT_OFF.equals(discountType)) { adjustmentValue = new Money(offerUnitValue, currency); } if (OfferDiscountType.FIX_PRICE.equals(discountType)) { adjustmentValue = currentPriceDetailValue.subtract(new Money(offerUnitValue, currency)); } if (OfferDiscountType.PERCENT_OFF.equals(discountType)) { BigDecimal offerValue = currentPriceDetailValue.getAmount() .multiply(offerUnitValue.divide(new BigDecimal("100"), 5, RoundingMode.HALF_EVEN)); if (rounding.isRoundOfferValues()) { offerValue = offerValue.setScale(rounding.getRoundingScale(), rounding.getRoundingMode()); } adjustmentValue = new Money(offerValue, currency); } if (currentPriceDetailValue.lessThan(adjustmentValue)) { adjustmentValue = currentPriceDetailValue; } return adjustmentValue; }
From source file:org.broadleafcommerce.core.offer.service.discount.domain.PromotableOfferUtility.java
public static Money computeAdjustmentValue(Money currentPriceDetailValue, BigDecimal offerUnitValue, OfferHolder offerHolder, PromotionRounding rounding) { Offer offer = offerHolder.getOffer(); BroadleafCurrency currency = offerHolder.getCurrency(); OfferDiscountType discountType = offer.getDiscountType(); Money adjustmentValue;/*from w w w . j a v a2s . co m*/ if (currency != null) { adjustmentValue = new Money(currency); } else { adjustmentValue = new Money(); } if (OfferDiscountType.AMOUNT_OFF.equals(discountType)) { adjustmentValue = new Money(offerUnitValue, currency); } if (OfferDiscountType.FIX_PRICE.equals(discountType)) { adjustmentValue = currentPriceDetailValue.subtract(new Money(offerUnitValue, currency)); } if (OfferDiscountType.PERCENT_OFF.equals(discountType)) { BigDecimal offerValue = currentPriceDetailValue.getAmount() .multiply(offerUnitValue.divide(new BigDecimal("100"), 5, RoundingMode.HALF_EVEN)); if (rounding.isRoundOfferValues()) { offerValue = offerValue.setScale(rounding.getRoundingScale(), rounding.getRoundingMode()); } adjustmentValue = new Money(offerValue, currency); } if (currentPriceDetailValue.lessThan(adjustmentValue)) { adjustmentValue = currentPriceDetailValue; } return adjustmentValue; }
From source file:ru.anr.base.BaseParent.java
/** * Short-cut for division with specific// www. jav a2 s .co m * * @param a * A divided value * @param b * A divisor * * @param scale * The scale * @return New {@link BigDecimal} in the specified scale */ public static final BigDecimal div(BigDecimal a, BigDecimal b, int scale) { return a.divide(b, scale, RoundingMode.HALF_UP); }
From source file:com.webbfontaine.valuewebb.model.util.Utils.java
public static BigDecimal divide(BigDecimal dividend, BigDecimal divisor, int scale) { BigDecimal retVal;// w ww. java2s.c o m if (dividend.doubleValue() == 0) { retVal = dividend; } else { if (divisor == null || divisor.doubleValue() == 0) { LOGGER.debug("Division by zero, result will be 'null' !!!"); retVal = null; } else { retVal = dividend.divide(divisor, scale, getRoundingMode()); } } return retVal; }
From source file:org.fenixedu.academic.domain.accounting.events.gratuity.ExternalScholarshipGratuityExemption.java
public BigDecimal calculateDiscountPercentage(Money amount) { final BigDecimal amountToDiscount = new BigDecimal(getValue().toString()); return amountToDiscount.divide(amount.getAmount(), 10, RoundingMode.HALF_EVEN); }