public PercentType[] toRGB() { PercentType red = null; PercentType green = null; PercentType blue = null; BigDecimal h = hue.divide(new BigDecimal(100), 10, BigDecimal.ROUND_HALF_UP); BigDecimal s = saturation.divide(new BigDecimal(100)); int h_int = h.multiply(new BigDecimal(5)) .divide(new BigDecimal(3), 10, BigDecimal.ROUND_HALF_UP) .intValue(); BigDecimal f = h.multiply(new BigDecimal(5)) .divide(new BigDecimal(3), 10, BigDecimal.ROUND_HALF_UP) .remainder(BigDecimal.ONE); PercentType a = new PercentType(value.multiply(BigDecimal.ONE.subtract(s))); PercentType b = new PercentType(value.multiply(BigDecimal.ONE.subtract(s.multiply(f)))); PercentType c = new PercentType( value.multiply(BigDecimal.ONE.subtract((BigDecimal.ONE.subtract(f)).multiply(s)))); if (h_int == 0 || h_int == 6) { red = getBrightness(); green = c; blue = a; } else if (h_int == 1) { red = b; green = getBrightness(); blue = a; } else if (h_int == 2) { red = a; green = getBrightness(); blue = c; } else if (h_int == 3) { red = a; green = b; blue = getBrightness(); } else if (h_int == 4) { red = c; green = a; blue = getBrightness(); } else if (h_int == 5) { red = getBrightness(); green = a; blue = b; } else { throw new RuntimeException(); } return new PercentType[] {red, green, blue}; }
private BigDecimal applyFee(boolean buy, BigDecimal price, BigDecimal transactionFee) { return (transactionFee.compareTo(BigDecimal.ZERO) == 0) ? price : price .multiply( (buy) ? BigDecimal.ONE.add(transactionFee) : BigDecimal.ONE.subtract(transactionFee)) .setScale(2, BigDecimal.ROUND_HALF_EVEN); }
private BigDecimal calcularDividendo() { // Dos apuestas de MONTO_APUESTA donde solo 1 es acertada. BigDecimal montoApostado = MONTO_APUESTA.multiply(new BigDecimal(2)); BigDecimal porcentajeARepartir = BigDecimal.ONE.subtract(BolsasApuestasManager.porcentajeComisionHipodromo); BigDecimal totalARepartir = montoApostado.multiply(porcentajeARepartir); BigDecimal dividendo = totalARepartir.divide(MONTO_APUESTA, 2, BigDecimal.ROUND_CEILING); if (BigDecimal.ONE.compareTo(dividendo) > 0) { dividendo = BigDecimal.ONE.setScale(2); } return dividendo; }
public BigDecimal calculate( MonetaryAmount annuity, MonetaryAmount paymentOrCashFlows, Rate rate, int periods) { MonetaryAmount pvAnnuity = PresentValueAnnuity.of().calculate(annuity, rate, periods); return new BigDecimal( String.valueOf( Math.log( BigDecimal.ONE .subtract( pvAnnuity .divide(paymentOrCashFlows.getNumber()) .getNumber() .numberValue(BigDecimal.class)) .pow(-1) .doubleValue()) / Math.log(1 + rate.get().doubleValue()))); }
protected boolean createTransactionFromChargeApplication( StepExecution<RatingTicket> stepExecution, ChargeApplication chargeApplication) { TaskExecution<RatingTicket> taskExecution = stepExecution.getTaskExecution(); try { Integer usageQuantity = chargeApplication.getQuantity(); if (usageQuantity == null) usageQuantity = 0; PricePlanMatrix ratePrice = (PricePlanMatrix) stepExecution.getParameter(VertinaConstants.RATE_PLAN); DiscountPlanMatrix discountPrice = (DiscountPlanMatrix) stepExecution.getParameter(VertinaConstants.DISCOUNT_PLAN); BigDecimal unitPrice1 = chargeApplication.getAmountWithoutTax(); boolean overriddenPrice = (unitPrice1 != null); if (!overriddenPrice) { if (ratePrice == null) { logger.error( "Error getting pricePlan for ChargeCode=" + chargeApplication.getChargeCode()); setNotAccepted(stepExecution, "ERROR_GETTING_PRICE"); return false; } else { logger.info( "found ratePrice:" + ratePrice.getId() + " price=" + ratePrice.getAmountWithoutTax() + " price2=" + ratePrice.getAmountWithoutTax2()); unitPrice1 = ratePrice.getAmountWithoutTax(); } } if (unitPrice1 == null) { unitPrice1 = BigDecimal.ZERO; } BigDecimal unitPrice2 = chargeApplication.getAmount2(); if (unitPrice2 == null && ratePrice != null && ratePrice.getAmountWithoutTax2() != null) { unitPrice2 = ratePrice.getAmountWithoutTax2(); } if (unitPrice2 == null) { unitPrice2 = BigDecimal.ZERO; } BigDecimal unitPriceRatio = BigDecimal.ONE; // subscription prorata if (ApplicationTypeEnum.PRORATA_SUBSCRIPTION.equals(chargeApplication.getApplicationType())) { try { if ("1".equals(chargeApplication.getParameter3())) { unitPriceRatio = new BigDecimal(chargeApplication.getParameter1()); } unitPrice1 = unitPrice1.multiply(unitPriceRatio); unitPrice2 = unitPrice2.multiply(unitPriceRatio); } catch (Exception e) { // TODO reject on failure? logger.error("Error calculating unit prices.", e); } } if (ApplicationTypeEnum.PRORATA_TERMINATION.equals(chargeApplication.getApplicationType())) { try { unitPriceRatio = new BigDecimal(chargeApplication.getParameter1()); unitPrice1 = unitPrice1.multiply(unitPriceRatio); unitPrice2 = unitPrice2.multiply(unitPriceRatio); } catch (Exception e) { // TODO reject on failure? logger.error("Error calculating unit prices.", e); } } Provider provider = chargeApplication.getProvider(); if (provider.getRounding() != null && provider.getRounding() > 0) { unitPrice1 = NumberUtils.round(unitPrice1, provider.getRounding()); unitPrice2 = NumberUtils.round(unitPrice2, provider.getRounding()); } BigDecimal amount1 = new BigDecimal(usageQuantity).multiply(unitPrice1); BigDecimal amount2 = new BigDecimal(usageQuantity).multiply(unitPrice2); BigDecimal amount1Discounted = BigDecimal.ZERO; BigDecimal amount2Discounted = BigDecimal.ZERO; if (overriddenPrice && discountPrice != null) { try { BigDecimal discount = BigDecimal.ONE.subtract(discountPrice.getPercent().divide(HUNDRED)); amount1Discounted = amount1.multiply(discount); amount2Discounted = amount2.multiply(discount); } catch (Exception e) { // TODO reject on failure? logger.error("Error calculating discount.", e); } } else { amount1Discounted = amount1; amount2Discounted = amount2; } BigDecimal amount1Tax = BigDecimal.ZERO; BigDecimal amount2Tax = BigDecimal.ZERO; if (chargeApplication.getTaxPercent() != null) { amount1Tax = amount1Discounted.multiply(chargeApplication.getTaxPercent()).divide(HUNDRED); amount2Tax = amount2Discounted.multiply(chargeApplication.getTaxPercent().divide(HUNDRED)); } RatedTransaction transaction = new RatedTransaction(); transaction.setProvider(chargeApplication.getProvider()); transaction.setChargeApplication(chargeApplication); // FIXME: Too many requests to get the wallet : copy wallet in // chargeApplication transaction.setWallet(chargeApplication.getSubscription().getUserAccount().getWallet()); transaction.setUsageCode(chargeApplication.getChargeCode()); transaction.setDescription( chargeApplication.getDescription() + (chargeApplication.getParameter2() == null ? "" : (" " + chargeApplication.getParameter2()))); transaction.setUsageDate(chargeApplication.getApplicationDate()); transaction.setUsageQuantity(usageQuantity); transaction.setUnitPrice1(unitPrice1); transaction.setUnitPrice2(unitPrice2); transaction.setUnitPriceRatio(unitPriceRatio); transaction.setDiscountPercent(discountPrice != null ? discountPrice.getPercent() : null); transaction.setInvoiceSubCategory(chargeApplication.getInvoiceSubCategory()); transaction.setTaxCode(chargeApplication.getTaxCode()); transaction.setTaxPercent(chargeApplication.getTaxPercent()); BigDecimal amount1WithTax = amount1Tax.add(amount1Discounted); BigDecimal amount2WithTax = amount2Tax.add(amount2Discounted); if (provider.getRounding() != null && provider.getRounding() > 0) { amount1Discounted = NumberUtils.round(amount1Discounted, provider.getRounding()); amount1WithTax = NumberUtils.round(amount1WithTax, provider.getRounding()); amount2Discounted = NumberUtils.round(amount2Discounted, provider.getRounding()); amount2WithTax = NumberUtils.round(amount2WithTax, provider.getRounding()); } transaction.setAmount1(amount1); transaction.setAmount1WithoutTax(amount1Discounted); // en transaction.setAmount1Tax(amount1Tax); transaction.setAmount1WithTax(amount1WithTax); transaction.setAmount2(amount2); transaction.setAmount2WithoutTax(amount2Discounted); transaction.setAmount2Tax(amount2Tax); transaction.setAmount2WithTax(amount2WithTax); transaction.setProvider(chargeApplication.getProvider()); transaction.setParameter1(chargeApplication.getCriteria1()); transaction.setParameter2(chargeApplication.getCriteria2()); transaction.setParameter3(chargeApplication.getCriteria3()); transaction.setParameter4(RatingTicket.sdf.format(chargeApplication.getSubscriptionDate())); transaction.setParameter5( chargeApplication.getAmountWithoutTax() != null ? chargeApplication.getAmountWithoutTax().toString() : null); transaction.setStatus(RatedTransactionStatusEnum.OPEN); putToTaskExecutionListContextParameter( VertinaConstants.LIST_OF_TRANSACTIONS_KEY, transaction, taskExecution); putToTaskExecutionListContextParameter( VertinaConstants.PROCESSED_CHARGE_APPLICATIONS_KEY, chargeApplication, taskExecution); } catch (Exception e) { logger.error("Error creating RatedTransaction", e); setNotAccepted(stepExecution, "ERROR_CREATING_TRANSACTION"); return false; } return true; }
protected boolean updateTransaction( StepExecution<RatingTicket> stepExecution, RatedTransaction transaction) { TaskExecution<RatingTicket> taskExecution = stepExecution.getTaskExecution(); RatingTicket ticket = stepExecution.getTicket(); try { PricePlanMatrix ratePrice = (PricePlanMatrix) stepExecution.getParameter(VertinaConstants.RATE_PLAN); DiscountPlanMatrix discountPrice = (DiscountPlanMatrix) stepExecution.getParameter(VertinaConstants.DISCOUNT_PLAN); BigDecimal unitPrice1 = ticket.amountWithoutTax; boolean overriddenPrice = (unitPrice1 != null); if (!overriddenPrice) { if (ratePrice == null) { logger.error("Error getting pricePlan for ChargeCode=" + ticket.chargeCode); setNotAccepted(stepExecution, "ERROR_GETTING_PRICE"); return false; } else { logger.info( "found ratePrice:" + ratePrice.getId() + " price=" + ratePrice.getAmountWithoutTax() + " price2=" + ratePrice.getAmountWithoutTax2()); unitPrice1 = ratePrice.getAmountWithoutTax(); } if (unitPrice1 == null) { unitPrice1 = BigDecimal.ZERO; } BigDecimal unitPrice2 = ticket.amount2; if (unitPrice2 == null && ratePrice != null && ratePrice.getAmountWithoutTax2() != null) { unitPrice2 = ratePrice.getAmountWithoutTax2(); } if (unitPrice2 == null) { unitPrice2 = BigDecimal.ZERO; } unitPrice1 = unitPrice1.multiply(transaction.getUnitPriceRatio()); unitPrice2 = unitPrice2.multiply(transaction.getUnitPriceRatio()); unitPrice1 = unitPrice1.setScale(4, RoundingMode.HALF_UP); BigDecimal amount1 = new BigDecimal(transaction.getUsageQuantity()).multiply(unitPrice1); unitPrice2 = unitPrice2.setScale(4, RoundingMode.HALF_UP); BigDecimal amount2 = new BigDecimal(transaction.getUsageQuantity()).multiply(unitPrice2); BigDecimal amount1Discounted = BigDecimal.ZERO; BigDecimal amount2Discounted = BigDecimal.ZERO; if (overriddenPrice && discountPrice != null) { try { BigDecimal discount = BigDecimal.ONE.subtract(discountPrice.getPercent().divide(HUNDRED)); amount1Discounted = amount1.multiply(discount); amount2Discounted = amount2.multiply(discount); } catch (Exception e) { // TODO reject on failure? logger.error("Error calculating discount.", e); } } else { amount1Discounted = amount1; amount2Discounted = amount2; } BigDecimal amount1Tax = BigDecimal.ZERO; BigDecimal amount2Tax = BigDecimal.ZERO; if (transaction.getTaxPercent() != null) { amount1Tax = amount1Discounted.multiply(transaction.getTaxPercent()).divide(HUNDRED); amount2Tax = amount2Discounted.multiply(transaction.getTaxPercent().divide(HUNDRED)); } BigDecimal amount1WithTax = amount1Tax.add(amount1Discounted); BigDecimal amount2WithTax = amount2Tax.add(amount2Discounted); Provider provider = transaction.getProvider(); if (provider != null && (provider.getRounding() != null && provider.getRounding() > 0)) { amount1Discounted = NumberUtils.round(amount1Discounted, provider.getRounding()); amount1WithTax = NumberUtils.round(amount1WithTax, provider.getRounding()); amount2Discounted = NumberUtils.round(amount2Discounted, provider.getRounding()); amount2WithTax = NumberUtils.round(amount2WithTax, provider.getRounding()); } transaction.setUnitPrice1(unitPrice1); transaction.setUnitPrice2(unitPrice2); transaction.setDiscountPercent(discountPrice != null ? discountPrice.getPercent() : null); transaction.setAmount1(amount1); transaction.setAmount1WithoutTax(amount1Discounted); // en transaction.setAmount1Tax(amount1Tax); transaction.setAmount1WithTax(amount1WithTax); transaction.setAmount2(amount2); transaction.setAmount2WithoutTax(amount2Discounted); transaction.setAmount2Tax(amount2Tax); transaction.setAmount2WithTax(amount2WithTax); } transaction.setStatus(RatedTransactionStatusEnum.OPEN); putToTaskExecutionListContextParameter( VertinaConstants.LIST_OF_TRANSACTIONS_KEY, transaction, taskExecution); } catch (Exception e) { logger.error("Error updating RatedTransaction", e); setNotAccepted(stepExecution, "ERROR_UPDATING_TRANSACTION"); return false; } return true; }
public BigDecimal value() { return BigDecimal.ONE .subtract(divident.divide(divisor, 4, RoundingMode.HALF_UP)) .multiply(HUNDRED) .setScale(2); }
public static BigDecimal sqrt(BigDecimal squarD, int scalar) { // Static constants - perhaps initialize in class Vladimir! BigDecimal TWO = new BigDecimal(2); double SQRT_10 = 3.162277660168379332; MathContext rootMC = new MathContext(scalar); // General number and precision checking int sign = squarD.signum(); if (sign == -1) throw new ArithmeticException("\nSquare root of a negative number: " + squarD); else if (sign == 0) return squarD.round(rootMC); int prec = rootMC.getPrecision(); // the requested precision if (prec == 0) throw new IllegalArgumentException("\nMost roots won't have infinite precision = 0"); // Initial precision is that of double numbers 2^63/2 ~ 4E18 int BITS = 62; // 63-1 an even number of number bits int nInit = 16; // precision seems 16 to 18 digits MathContext nMC = new MathContext(18, RoundingMode.HALF_UP); // Iteration variables, for the square root x and the reciprocal v BigDecimal x = null, e = null; // initial x: x0 ~ sqrt() BigDecimal v = null, g = null; // initial v: v0 = 1/(2*x) // Estimate the square root with the foremost 62 bits of squarD BigInteger bi = squarD.unscaledValue(); // bi and scale are a tandem int biLen = bi.bitLength(); int shift = Math.max(0, biLen - BITS + (biLen % 2 == 0 ? 0 : 1)); // even // shift.. bi = bi.shiftRight(shift); // ..floors to 62 or 63 bit BigInteger double root = Math.sqrt(bi.doubleValue()); BigDecimal halfBack = new BigDecimal(BigInteger.ONE.shiftLeft(shift / 2)); int scale = squarD.scale(); if (scale % 2 == 1) // add half scales of the root to odds.. root *= SQRT_10; // 5 -> 2, -5 -> -3 need half a scale more.. scale = (int) Math.floor(scale / 2.); // ..where 100 -> 10 shifts the // scale // Initial x - use double root - multiply by halfBack to unshift - set // new scale x = new BigDecimal(root, nMC); x = x.multiply(halfBack, nMC); // x0 ~ sqrt() if (scale != 0) x = x.movePointLeft(scale); if (prec < nInit) // for prec 15 root x0 must surely be OK return x.round(rootMC); // return small prec roots without // iterations // Initial v - the reciprocal v = BigDecimal.ONE.divide(TWO.multiply(x), nMC); // v0 = 1/(2*x) // Collect iteration precisions beforehand ArrayList<Integer> nPrecs = new ArrayList<Integer>(); assert nInit > 3 : "Never ending loop!"; // assume nInit = 16 <= prec // Let m be the exact digits precision in an earlier! loop for (int m = prec + 1; m > nInit; m = m / 2 + (m > 100 ? 1 : 2)) nPrecs.add(m); // The loop of "Square Root by Coupled Newton Iteration" for simpletons for (int i = nPrecs.size() - 1; i > -1; i--) { // Increase precision - next iteration supplies n exact digits nMC = new MathContext( nPrecs.get(i), (i % 2 == 1) ? RoundingMode.HALF_UP : RoundingMode.HALF_DOWN); // Next x // e = d - x^2 e = squarD.subtract(x.multiply(x, nMC), nMC); if (i != 0) x = x.add(e.multiply(v, nMC)); // x += e*v ~ sqrt() else { x = x.add(e.multiply(v, rootMC), rootMC); // root x is ready! break; } // Next v // g = 1 - 2*x*v g = BigDecimal.ONE.subtract(TWO.multiply(x).multiply(v, nMC)); v = v.add(g.multiply(v, nMC)); // v += g*v ~ 1/2/sqrt() } return x; // return sqrt(squarD) with precision of rootMC }