List of usage examples for java.math BigDecimal ZERO
BigDecimal ZERO
To view the source code for java.math BigDecimal ZERO.
Click Source Link
From source file:Main.java
/** * Divides two decimals and applies the given scale and a ROUND_HALF_UP. This method should be used only for the final result * calculation. For example if we have something like this: (axb)/c the rules should be applied to the result of the division * only and not all the computations that give us the final result. * //from w ww . j a v a2s . c o m * @param dividend the dividend * @param divisor the divisor * @param scale the scale for the result * @return the result of the division after the scale and rounding are applied */ public static BigDecimal divide(BigDecimal dividend, BigDecimal divisor, int scale) { BigDecimal result = BigDecimal.ZERO; if (dividend != null && divisor != null && !divisor.equals(BigDecimal.ZERO)) { result = dividend.divide(divisor, scale, BigDecimal.ROUND_HALF_UP); } return result; }
From source file:Main.java
/** * Multiplies two decimals and applies the given scale and a ROUND_HALF_UP. This method should be used only for the final result * calculation.For example if we have something like this: (axb)/c the rules should be applied to the result of the division * only and not all the computations that give us the final result. * //from w w w . j av a 2s . co m * @param multiplier1 first multiplier * @param multiplier2 second multiplier * @param scale the scale fo the result * @return the result of the multiplication after scale and rounding are applied */ public static BigDecimal multiply(BigDecimal multiplier1, BigDecimal multiplier2, int scale) { BigDecimal result = BigDecimal.ZERO; if (multiplier1 != null && multiplier2 != null) { result = multiplier1.multiply(multiplier2); } result = result.setScale(scale, BigDecimal.ROUND_HALF_UP); return result; }
From source file:Main.java
@NonNull public static BigDecimal getNonNull(BigDecimal value) { return getNonNull(value, BigDecimal.ZERO); }
From source file:Main.java
public static BigDecimal log10(BigDecimal b) { final int NUM_OF_DIGITS = SCALE + 2; // need to add one to get the right number of dp // and then add one again to get the next number // so I can round it correctly. MathContext mc = new MathContext(NUM_OF_DIGITS, RoundingMode.HALF_EVEN); //special conditions: // log(-x) -> exception // log(1) == 0 exactly; // log of a number lessthan one = -log(1/x) if (b.signum() <= 0) { throw new ArithmeticException("log of a negative number! (or zero)"); } else if (b.compareTo(BigDecimal.ONE) == 0) { return BigDecimal.ZERO; } else if (b.compareTo(BigDecimal.ONE) < 0) { return (log10((BigDecimal.ONE).divide(b, mc))).negate(); }/*from w w w. ja v a2 s. com*/ StringBuilder sb = new StringBuilder(); //number of digits on the left of the decimal point int leftDigits = b.precision() - b.scale(); //so, the first digits of the log10 are: sb.append(leftDigits - 1).append("."); //this is the algorithm outlined in the webpage int n = 0; while (n < NUM_OF_DIGITS) { b = (b.movePointLeft(leftDigits - 1)).pow(10, mc); leftDigits = b.precision() - b.scale(); sb.append(leftDigits - 1); n++; } BigDecimal ans = new BigDecimal(sb.toString()); //Round the number to the correct number of decimal places. ans = ans.round(new MathContext(ans.precision() - ans.scale() + SCALE, RoundingMode.HALF_EVEN)); return ans; }
From source file:Main.java
public static BigDecimal log10(BigDecimal b) { final int NUM_OF_DIGITS = SCALE + 2; // need to add one to get the right number of dp // and then add one again to get the next number // so I can round it correctly. MathContext mc = new MathContext(NUM_OF_DIGITS, RoundingMode.HALF_EVEN); // special conditions: // log(-x) -> exception // log(1) == 0 exactly; // log of a number lessthan one = -log(1/x) if (b.signum() <= 0) { throw new ArithmeticException("log of a negative number! (or zero)"); } else if (b.compareTo(BigDecimal.ONE) == 0) { return BigDecimal.ZERO; } else if (b.compareTo(BigDecimal.ONE) < 0) { return (log10((BigDecimal.ONE).divide(b, mc))).negate(); }/*from w ww .j ava 2s . c o m*/ StringBuilder sb = new StringBuilder(); // number of digits on the left of the decimal point int leftDigits = b.precision() - b.scale(); // so, the first digits of the log10 are: sb.append(leftDigits - 1).append("."); // this is the algorithm outlined in the webpage int n = 0; while (n < NUM_OF_DIGITS) { b = (b.movePointLeft(leftDigits - 1)).pow(10, mc); leftDigits = b.precision() - b.scale(); sb.append(leftDigits - 1); n++; } BigDecimal ans = new BigDecimal(sb.toString()); // Round the number to the correct number of decimal places. ans = ans.round(new MathContext(ans.precision() - ans.scale() + SCALE, RoundingMode.HALF_EVEN)); return ans; }
From source file:com.ebay.cloud.cms.service.resources.impl.MetadataValidator.java
public static void validateNumber(Number value, String key) { if (value != null) { CheckConditions.checkArgument(new BigDecimal(value.toString()).compareTo(BigDecimal.ZERO) > 0, "Configuration value must be greater than 0 for configuration item %s!", key); }/*w w w .j a va2s . co m*/ }
From source file:com.qcadoo.model.api.BigDecimalUtils.java
/** * Converts value, if null returns zero//from w w w . j a va2 s . c om * * @param value * value * * @return value or zero */ public static BigDecimal convertNullToZero(final Object value) { if (value == null) { return BigDecimal.ZERO; } if (value instanceof BigDecimal) { return (BigDecimal) value; } return BigDecimal.valueOf(Double.valueOf(value.toString())); }
From source file:fr.francetelecom.callback.ItemReaderFooter.java
@Override public void write(List<? extends Report> items) throws Exception { BigDecimal chunkTotal = BigDecimal.ZERO; for (Report report : items) { System.out.println("footer : " + report); }/*from w w w.jav a 2 s . com*/ delegate.write(items); // After successfully writing all items totalAmount = totalAmount.add(chunkTotal); }
From source file:com.stitchgalaxy.domain.service.SitchGalaxyService.java
@Transactional(rollbackFor = Exception.class) public void addProduct() { Product p = new Product(); p.setBlocked(Boolean.TRUE);/*from w w w.j a v a2 s . com*/ p.setPrice(BigDecimal.ZERO); p.setDate(new LocalDate(2014, 1, 1)); productRepository.save(p); }
From source file:com.github.fge.jsonschema.keyword.digest.helpers.NumericDigester.java
private static boolean valueIsLong(final JsonNode node) { if (!node.canConvertToLong()) return false; if (NodeType.getNodeType(node) == NodeType.INTEGER) return true; return node.decimalValue().remainder(BigDecimal.ONE).compareTo(BigDecimal.ZERO) == 0; }