List of usage examples for java.math BigDecimal divide
public BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode)
From source file:org.openvpms.archetype.rules.math.MathRules.java
/** * Performs a division, rounding the result to the specified no. of places. * * @param dividend the value to divide// w ww. j a va 2 s . c o m * @param divisor the divisor * @param scale the no. of decimal places * @return the divided value */ public static BigDecimal divide(BigDecimal dividend, BigDecimal divisor, int scale) { return dividend.divide(divisor, scale, RoundingMode.HALF_UP); }
From source file:Main.java
public static String formatByte(long l) { String s = "0 KB"; if (l <= 0x100000L) { BigDecimal bigdecimal = new BigDecimal(l); BigDecimal bigdecimal1 = new BigDecimal(1024); RoundingMode roundingmode = RoundingMode.CEILING; String s1 = String.valueOf(bigdecimal.divide(bigdecimal1, 0, roundingmode).toString()); s = (new StringBuilder(s1)).append(" KB").toString(); } else {//from ww w . j av a2 s. com if (l >= 0x40000000L) { BigDecimal bigdecimal2 = new BigDecimal(l); BigDecimal bigdecimal3 = new BigDecimal(0x40000000); RoundingMode roundingmode1 = RoundingMode.CEILING; String s2 = String.valueOf(bigdecimal2.divide(bigdecimal3, 2, roundingmode1).toString()); s = (new StringBuilder(s2)).append(" G").toString(); } else { BigDecimal bigdecimal4 = new BigDecimal(l); BigDecimal bigdecimal5 = new BigDecimal(0x100000); RoundingMode roundingmode2 = RoundingMode.CEILING; String s3 = String.valueOf(bigdecimal4.divide(bigdecimal5, 2, roundingmode2).toString()); s = (new StringBuilder(s3)).append(" MB").toString(); } } return s; }
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. * // w ww.j av a 2 s . co 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
public static BigDecimal calculatePercent(BigDecimal numerator, BigDecimal denominator) { BigDecimal result = BigDecimal.ZERO; if (numerator != null && denominator != null && (denominator.compareTo(BigDecimal.ZERO) != 0)) { result = numerator.divide(denominator, 3, BigDecimal.ROUND_HALF_UP).movePointRight(2); }/*from w w w . j av a2 s . c o m*/ return result; }
From source file:BigDSqrt.java
public static BigDecimal sqrt(BigDecimal n, int s) { BigDecimal TWO = BigDecimal.valueOf(2); // Obtain the first approximation BigDecimal x = n.divide(BigDecimal.valueOf(3), s, BigDecimal.ROUND_DOWN); BigDecimal lastX = BigDecimal.valueOf(0); // Proceed through 50 iterations for (int i = 0; i < 50; i++) { x = n.add(x.multiply(x)).divide(x.multiply(TWO), s, BigDecimal.ROUND_DOWN); if (x.compareTo(lastX) == 0) break; lastX = x;/*from w w w. j a va2 s . c om*/ } return x; }
From source file:com.fengduo.bee.commons.util.NumberParser.java
public static double div(double a, double b, int scale) { if (scale < 0) { throw new IllegalArgumentException("The scale must be a positive integer or zero"); }//from w w w. ja v a 2 s . c om BigDecimal b1 = new BigDecimal(Double.toString(a)); BigDecimal b2 = new BigDecimal(Double.toString(b)); return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue(); }
From source file:com.github.sarxos.xchange.impl.FetchOpenExchangeImpl.java
private static Collection<ExchangeRate> read(String json, String to, Collection<String> from) throws JsonProcessingException, IOException { // example JSON // {/*from w w w. j av a2 s . co m*/ // "disclaimer": "...", // "license": "...", // "timestamp": 1427716861, // "base": "USD", // "rates": { // "AED": 3.67305, // "AFN": 57.780167, // "ALL": 129.4859, // "AMD": 471.586001, // "ANG": 1.78948, // "AOA": 107.97125, // ... etc for more symbols LOG.trace("Reading JSON tree: {}", json); JsonNode root = MAPPER.readTree(json); if (root == null) { throw new IOException("Invalid JSON received: " + json); } JsonNode rates = root.get("rates"); if (rates == null) { throw new IOException("No rate element has been found in received JSON: " + json); } Iterator<Entry<String, JsonNode>> entries = rates.fields(); Map<String, BigDecimal> currencies = new HashMap<>(); while (entries.hasNext()) { Entry<String, JsonNode> entry = entries.next(); String symbol = entry.getKey(); String value = entry.getValue().asText(); currencies.put(symbol, new BigDecimal(value)); } BigDecimal base = currencies.get(to); Set<ExchangeRate> exchangerates = new HashSet<>(); for (Entry<String, BigDecimal> entry : currencies.entrySet()) { String symbol = entry.getKey(); if (!from.contains(symbol)) { continue; } BigDecimal rate = entry.getValue(); BigDecimal newrate = rate.divide(base, 8, RoundingMode.HALF_EVEN); exchangerates.add(new ExchangeRate(to + symbol, newrate.toString())); } return exchangerates; }
From source file:wzw.util.NumberUtils.java
/** * //from w w w. j a va 2 s .co m * ?????? * * @param v * ?? * * @param scale * ???? * * @return ?? * */ public static double round(double v, int scale) { if (scale < 0) { throw new IllegalArgumentException("The scale must be a positive integer or zero"); } BigDecimal b = new BigDecimal(Double.toString(v)); BigDecimal one = new BigDecimal("1"); return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue(); }
From source file:Main.java
/** * Compute e^x to a given scale./* w w w .j a v a 2 s. c o m*/ * Break x into its whole and fraction parts and * compute (e^(1 + fraction/whole))^whole using Taylor's formula. * @param x the value of x * @param scale the desired scale of the result * @return the result value */ public static BigDecimal exp(BigDecimal x, int scale) { // e^0 = 1 if (x.signum() == 0) { return BigDecimal.valueOf(1); } // If x is negative, return 1/(e^-x). else if (x.signum() == -1) { return BigDecimal.valueOf(1).divide(exp(x.negate(), scale), scale, BigDecimal.ROUND_HALF_EVEN); } // Compute the whole part of x. BigDecimal xWhole = x.setScale(0, BigDecimal.ROUND_DOWN); // If there isn't a whole part, compute and return e^x. if (xWhole.signum() == 0) return expTaylor(x, scale); // Compute the fraction part of x. BigDecimal xFraction = x.subtract(xWhole); // z = 1 + fraction/whole BigDecimal z = BigDecimal.valueOf(1).add(xFraction.divide(xWhole, scale, BigDecimal.ROUND_HALF_EVEN)); // t = e^z BigDecimal t = expTaylor(z, scale); BigDecimal maxLong = BigDecimal.valueOf(Long.MAX_VALUE); BigDecimal result = BigDecimal.valueOf(1); // Compute and return t^whole using intPower(). // If whole > Long.MAX_VALUE, then first compute products // of e^Long.MAX_VALUE. while (xWhole.compareTo(maxLong) >= 0) { result = result.multiply(intPower(t, Long.MAX_VALUE, scale)).setScale(scale, BigDecimal.ROUND_HALF_EVEN); xWhole = xWhole.subtract(maxLong); Thread.yield(); } return result.multiply(intPower(t, xWhole.longValue(), scale)).setScale(scale, BigDecimal.ROUND_HALF_EVEN); }
From source file:wzw.util.NumberUtils.java
/** //from www . j a v a 2s. c o m * ?????scale? * ?? * @param v1 * @param v2 * @param scale ???? * @return ? */ public static double div(double v1, double v2, int scale) { if (scale < 0) { throw new IllegalArgumentException("The scale must be a positive integer or zero"); } BigDecimal b1 = new BigDecimal(Double.toString(v1)); BigDecimal b2 = new BigDecimal(Double.toString(v2)); return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue(); }