List of usage examples for java.math BigDecimal multiply
public BigDecimal multiply(BigDecimal multiplicand)
(this × multiplicand)
, and whose scale is (this.scale() + multiplicand.scale()) . From source file:Main.java
public static int kgToML(float paramFloat) { BigDecimal localBigDecimal1 = new BigDecimal(String.valueOf(paramFloat)); BigDecimal localBigDecimal2 = new BigDecimal("63701"); BigDecimal localBigDecimal3 = new BigDecimal("65536"); return new BigDecimal(String.valueOf(localBigDecimal1.multiply(localBigDecimal2).doubleValue())) .divide(localBigDecimal3, 0, 4).intValue(); }
From source file:Main.java
public static BigDecimal calculateDiscountAmt(BigDecimal Discount, BigDecimal DiscountAmt) { if (DiscountAmt == null || Discount.compareTo(BigDecimal.ZERO) == 0) return BigDecimal.ZERO; else {/*from ww w.j av a 2s.c om*/ BigDecimal discountPerc = Discount.divide(new BigDecimal(100)); return DiscountAmt.multiply(discountPerc); } }
From source file:Util.java
public static BigDecimal[] quadratic(BigDecimal a, BigDecimal b, BigDecimal c, RoundingMode rounding) { // ax^2 + bx + c = 0 BigDecimal part1 = b.negate(); BigDecimal part2 = sqrt(b.multiply(b).subtract(BigDecimalFOUR.multiply(a).multiply(c)), rounding); BigDecimal part3 = BigDecimalTWO.multiply(a); BigDecimal[] toRet = new BigDecimal[2]; toRet[0] = (part1.subtract(part2)).divide(part3, RoundingMode.CEILING); toRet[1] = (part1.add(part2)).divide(part3, RoundingMode.CEILING); return toRet; }
From source file:com.fengduo.bee.commons.util.NumberParser.java
public static double mul(double a, double b) { BigDecimal b1 = new BigDecimal(Double.toString(a)); BigDecimal b2 = new BigDecimal(Double.toString(b)); return b1.multiply(b2).doubleValue(); }
From source file:Main.java
/** Returns Map with total, squaredTotal, count, average, stdDev, maximum; fieldName field in Maps must have type BigDecimal; * if count of non-null fields is less than 2 returns null as cannot calculate a standard deviation */ public static Map<String, BigDecimal> stdDevMaxFromMapField(List<Map<String, Object>> dataList, String fieldName, BigDecimal stdDevMultiplier) { BigDecimal total = BigDecimal.ZERO; BigDecimal squaredTotal = BigDecimal.ZERO; int count = 0; for (Map<String, Object> dataMap : dataList) { if (dataMap == null) continue; BigDecimal value = (BigDecimal) dataMap.get(fieldName); if (value == null) continue; total = total.add(value);//from ww w . j a v a2 s. c o m squaredTotal = squaredTotal.add(value.multiply(value)); count++; } if (count < 2) return null; BigDecimal countBd = new BigDecimal(count); BigDecimal average = total.divide(countBd, BigDecimal.ROUND_HALF_UP); double totalDouble = total.doubleValue(); BigDecimal stdDev = new BigDecimal(Math .sqrt(Math.abs(squaredTotal.doubleValue() - ((totalDouble * totalDouble) / count)) / (count - 1))); Map<String, BigDecimal> retMap = new HashMap<>(6); retMap.put("total", total); retMap.put("squaredTotal", squaredTotal); retMap.put("count", countBd); retMap.put("average", average); retMap.put("stdDev", stdDev); if (stdDevMultiplier != null) retMap.put("maximum", average.add(stdDev.multiply(stdDevMultiplier))); return retMap; }
From source file:Main.java
/** * Compute x^exponent to a given scale. Uses the same algorithm as class * numbercruncher.mathutils.IntPower./*from w ww. j av a 2s . co m*/ * * @param x * the value x * @param exponent * the exponent value * @param scale * the desired scale of the result * @return the result value */ public static BigDecimal intPower(BigDecimal x, long exponent, int scale) { // If the exponent is negative, compute 1/(x^-exponent). if (exponent < 0) { return BigDecimal.valueOf(1).divide(intPower(x, -exponent, scale), scale, BigDecimal.ROUND_HALF_EVEN); } BigDecimal power = BigDecimal.valueOf(1); // Loop to compute value^exponent. while (exponent > 0) { // Is the rightmost bit a 1? if ((exponent & 1) == 1) { power = power.multiply(x).setScale(scale, BigDecimal.ROUND_HALF_EVEN); } // Square x and shift exponent 1 bit to the right. x = x.multiply(x).setScale(scale, BigDecimal.ROUND_HALF_EVEN); exponent >>= 1; Thread.yield(); } return power; }
From source file:alluxio.util.FormatUtils.java
/** * Parses a String size to Bytes.//from w ww .j a va 2 s .c om * * @param spaceSize the size of a space, e.g. 10GB, 5TB, 1024 * @return the space size in bytes */ public static long parseSpaceSize(String spaceSize) { double alpha = 0.0001; String ori = spaceSize; String end = ""; int index = spaceSize.length() - 1; while (index >= 0) { if (spaceSize.charAt(index) > '9' || spaceSize.charAt(index) < '0') { end = spaceSize.charAt(index) + end; } else { break; } index--; } spaceSize = spaceSize.substring(0, index + 1); double ret = Double.parseDouble(spaceSize); end = end.toLowerCase(); if (end.isEmpty() || end.equals("b")) { return (long) (ret + alpha); } else if (end.equals("kb")) { return (long) (ret * Constants.KB + alpha); } else if (end.equals("mb")) { return (long) (ret * Constants.MB + alpha); } else if (end.equals("gb")) { return (long) (ret * Constants.GB + alpha); } else if (end.equals("tb")) { return (long) (ret * Constants.TB + alpha); } else if (end.equals("pb")) { // When parsing petabyte values, we can't multiply with doubles and longs, since that will // lose presicion with such high numbers. Therefore we use a BigDecimal. BigDecimal pBDecimal = new BigDecimal(Constants.PB); return pBDecimal.multiply(BigDecimal.valueOf(ret)).longValue(); } else { throw new IllegalArgumentException("Fail to parse " + ori + " to bytes"); } }
From source file:org.cryptomath.util.NumberUtil.java
public static String absorbFloats(String value) { String result;//from w w w . j a v a2s . c o m if (value.matches("[\\d]*.[\\d]*")) { logger.info("Absorbing floats"); BigDecimal bd = new BigDecimal(value); int f = CryptoConfigSpec.getInstance().getMathConfig().getFractions(); // logger.info("Absorbed value::check" + value); // logger.info("Absorbed value::check" + bd); bd = bd.multiply(new BigDecimal("10").pow(f)); // logger.info("Absorbed value::check" + bd); String[] tokens = bd.toString().split("\\."); result = tokens[0]; // result = truncate(value, f).toString(); logger.info("Absorbed value::check::" + result); } else { throw new NumberFormatException(MessageFormat.format("Invalid argument {0}", value)); } return result; }
From source file:nu.mine.kino.projects.utils.Utils.java
static Date excelSerialValue2Date(String serial) { BigDecimal dec = new BigDecimal(serial).subtract(new BigDecimal("25569")).subtract(new BigDecimal("0.375")); BigDecimal ans = dec.multiply(new BigDecimal("86400000")); return new Date(ans.longValue()); }
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;/* ww w . j a v a2 s .co m*/ } return x; }