List of usage examples for java.math BigDecimal divide
public BigDecimal divide(BigDecimal divisor, MathContext mc)
From source file:Util.java
public static BigDecimal sqrt(BigDecimal number, BigDecimal guess, RoundingMode rounding) { BigDecimal result = BigDecimalZERO; BigDecimal flipA = result;//from w ww. j a va 2 s .co m BigDecimal flipB = result; boolean first = true; while (result.compareTo(guess) != 0) { if (!first) guess = result; else first = false; result = number.divide(guess, rounding).add(guess).divide(BigDecimalTWO, rounding); // handle flip flops if (result.equals(flipB)) return flipA; flipB = flipA; flipA = result; } return result; }
From source file:nl.strohalm.cyclos.utils.DateHelper.java
/** * Returns the number of days between 2 dates as a BigDecimal. Will be negative if date2 < date1, or positive otherwise. *//*from ww w .j a va 2 s .c o m*/ public static BigDecimal decimalDaysBetween(final Calendar date1, final Calendar date2) { if (date1 == null || date2 == null) { return BigDecimal.ZERO; } final BigDecimal difference = new BigDecimal(date2.getTimeInMillis() - date1.getTimeInMillis()); final MathContext mathContext = new MathContext(LocalSettings.MAX_PRECISION); final BigDecimal result = difference.divide(new BigDecimal(DateUtils.MILLIS_PER_DAY), mathContext); return result; }
From source file:org.kuali.kpme.core.util.TKUtils.java
public static BigDecimal convertMinutesToHours(BigDecimal minutes) { return minutes.divide(HrConstants.BIG_DECIMAL_60, HrConstants.MATH_CONTEXT); }
From source file:org.kuali.kpme.core.util.TKUtils.java
public static BigDecimal convertMillisToDays(long millis) { BigDecimal hrs = convertMillisToHours(millis); return hrs.divide(HrConstants.BIG_DECIMAL_HRS_IN_DAY, HrConstants.MATH_CONTEXT); }
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 va 2 s . c om*/ 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:br.msf.commons.util.AbstractDateUtils.java
public static BigDecimal getDifferenceInDays(final Object date1, final Object date2, final boolean ignoreTimeInfo) { ArgumentUtils.rejectIfAnyNull(date1, date2); Calendar c1 = toUtcCalendar(date1); Calendar c2 = toUtcCalendar(date2); if (ignoreTimeInfo) { c1 = getTimeTruncatedInternal(c1); c2 = getTimeTruncatedInternal(c2); }/* w w w. j a v a2 s .c om*/ final long l1 = c1.getTimeInMillis(); final long l2 = c2.getTimeInMillis(); BigDecimal res; if (l1 > l2) { res = new BigDecimal(l1 - l2); } else { res = new BigDecimal(l2 - l1); } return NumberUtils.round(res.divide(MILLISECS_PER_DAY, MathContext.DECIMAL128), 2); }
From source file:Main.java
/** * NOTE: Arithmetic operations in primitive types may lead to arithmetic overflow. To retain * precision, BigDecimal objects are used. * * @param rgb// w w w .j a v a2 s . c om * @return */ public static int[] convertRGB_8_8_8_To_HSB_32_32_32(int[] rgb) { int[] hsb = new int[3]; int maxChroma = Math.max(Math.max(rgb[0], rgb[1]), rgb[2]); int minChroma = Math.min(Math.min(rgb[0], rgb[1]), rgb[2]); int diff = maxChroma - minChroma; // Hue BigDecimal hue; if (diff == 0) { hue = BigDecimal.ZERO; } else if (maxChroma == rgb[0]) { float tmp = (rgb[1] - rgb[2]) / (float) diff; if (tmp < 0) { tmp += 6 * Math.ceil(-tmp / 6.0); } else { tmp -= 6 * Math.floor(tmp / 6.0); } hue = BigDecimal.valueOf(tmp); } else if (maxChroma == rgb[1]) { hue = BigDecimal.valueOf((rgb[2] - rgb[0]) / (float) diff + 2); } else { hue = BigDecimal.valueOf((rgb[0] - rgb[1]) / (float) diff + 4); } // [0, 360] -> [0, 0xffffffff] hue = hue.multiply(BigDecimal.valueOf(0xffffffffL)); hue = hue.divide(BigDecimal.valueOf(6), RoundingMode.FLOOR); hsb[0] = ByteBuffer.allocate(8).putLong(hue.longValue()).getInt(4); // Saturation if (maxChroma == 0) { hsb[1] = 0; } else { // [0, 1] -> [0, 0xffffffff] BigDecimal sat = BigDecimal.valueOf(diff); sat = sat.multiply(BigDecimal.valueOf(0xffffffffL)); sat = sat.divide(BigDecimal.valueOf(maxChroma), RoundingMode.FLOOR); hsb[1] = ByteBuffer.allocate(8).putLong(sat.longValue()).getInt(4); } // Brightness // [0, 255] -> [0, 0xffffffff] BigDecimal brightness = BigDecimal.valueOf(maxChroma); brightness = brightness.multiply(BigDecimal.valueOf(0xffffffffL)); brightness = brightness.divide(BigDecimal.valueOf(0xffL), RoundingMode.FLOOR); hsb[2] = ByteBuffer.allocate(8).putLong(brightness.longValue()).getInt(4); return hsb; }
From source file:com.liferay.util.FileUtil.java
public static String getsize(File fileName) { String finalVal;/*from w w w . j a va 2s. c om*/ long filesize = fileName.length(); BigDecimal size = new BigDecimal(filesize); BigDecimal byteVal = null; BigDecimal changedByteVal = null; finalVal = ""; if (filesize <= 0) { finalVal = ""; } else if (filesize < MEGA_BYTE) { byteVal = new BigDecimal(KILO_BYTE); if (size != null) { changedByteVal = size.divide(byteVal, MathContext.UNLIMITED); finalVal = Long.toString(Math.round(Math.ceil(changedByteVal.doubleValue()))) + " KB"; } } else if (filesize < GIGA_BYTE) { byteVal = new BigDecimal(MEGA_BYTE); if (size != null) { changedByteVal = size.divide(byteVal, MathContext.UNLIMITED); finalVal = Long.toString(Math.round(Math.ceil(changedByteVal.doubleValue()))) + " MB"; } } else if (filesize < TERA_BYTE) { byteVal = new BigDecimal(GIGA_BYTE); if (size != null) { changedByteVal = size.divide(byteVal, MathContext.UNLIMITED); finalVal = Long.toString(Math.round(Math.ceil(changedByteVal.doubleValue()))) + " GB"; } } else { byteVal = new BigDecimal(TERA_BYTE); if (size != null) { changedByteVal = size.divide(byteVal, MathContext.UNLIMITED); finalVal = Long.toString(Math.round(Math.ceil(changedByteVal.doubleValue()))) + " TB"; } } return finalVal; }
From source file:org.mifosplatform.portfolio.loanaccount.domain.LoanCharge.java
public static BigDecimal percentageOf(final BigDecimal value, final BigDecimal percentage) { BigDecimal percentageOf = BigDecimal.ZERO; if (isGreaterThanZero(value)) { final MathContext mc = new MathContext(8, RoundingMode.HALF_EVEN); final BigDecimal multiplicand = percentage.divide(BigDecimal.valueOf(100l), mc); percentageOf = value.multiply(multiplicand, mc); }/*from ww w.j a va 2 s. c om*/ return percentageOf; }
From source file:com.idylwood.utils.MathUtils.java
final public static double meanArbPrec(final double data[]) { BigDecimal mean = new BigDecimal(0); for (double x : data) mean = mean.add(new BigDecimal(x), MathContext.UNLIMITED); mean = mean.divide(new BigDecimal(data.length), MathContext.UNLIMITED); return mean.doubleValue(); }