Java Code Examples for java.math.BigDecimal#divideAndRemainder()

The following examples show how to use java.math.BigDecimal#divideAndRemainder() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Bats   File: RexLiteralImpl.java    License: Apache License 2.0 6 votes vote down vote up
private String intervalString(BigDecimal v) {
    final List<TimeUnit> timeUnits = getTimeUnits(type.getSqlTypeName());
    final StringBuilder b = new StringBuilder();
    for (TimeUnit timeUnit : timeUnits) {
        final BigDecimal[] result = v.divideAndRemainder(timeUnit.multiplier);
        if (b.length() > 0) {
            b.append(timeUnit.separator);
        }
        final int width = b.length() == 0 ? -1 : width(timeUnit); // don't pad 1st
        pad(b, result[0].toString(), width);
        v = result[1];
    }
    if (Util.last(timeUnits) == TimeUnit.MILLISECOND) {
        while (b.toString().matches(".*\\.[0-9]*0")) {
            if (b.toString().endsWith(".0")) {
                b.setLength(b.length() - 2); // remove ".0"
            } else {
                b.setLength(b.length() - 1); // remove "0"
            }
        }
    }
    return b.toString();
}
 
Example 2
Source Project: j2objc   File: BigDecimalArithmeticTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * divideAndRemainder(BigDecimal, MathContext)
 */
public void testDivideAndRemainderMathContextUP() {
    String a = "3736186567876876578956958765675671119238118911893939591735";
    int aScale = 45;
    String b = "134432345432345748766876876723342238476237823787879183470";
    int bScale = 70;
    int precision = 75;
    RoundingMode rm = RoundingMode.UP;
    MathContext mc = new MathContext(precision, rm);
    String res = "277923185514690367474770683";
    int resScale = 0;
    String rem = "1.3032693871288309587558885943391070087960319452465789990E-15";
    int remScale = 70;
    BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
    BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
    BigDecimal result[] = aNumber.divideAndRemainder(bNumber, mc);
    assertEquals("incorrect quotient value", res, result[0].toString());
    assertEquals("incorrect quotient scale", resScale, result[0].scale());
    assertEquals("incorrect remainder value", rem, result[1].toString());
    assertEquals("incorrect remainder scale", remScale, result[1].scale());
}
 
Example 3
Source Project: Quicksql   File: RexLiteral.java    License: MIT License 6 votes vote down vote up
private String intervalString(BigDecimal v) {
  final List<TimeUnit> timeUnits = getTimeUnits(type.getSqlTypeName());
  final StringBuilder b = new StringBuilder();
  for (TimeUnit timeUnit : timeUnits) {
    final BigDecimal[] result = v.divideAndRemainder(timeUnit.multiplier);
    if (b.length() > 0) {
      b.append(timeUnit.separator);
    }
    final int width = b.length() == 0 ? -1 : width(timeUnit); // don't pad 1st
    pad(b, result[0].toString(), width);
    v = result[1];
  }
  if (Util.last(timeUnits) == TimeUnit.MILLISECOND) {
    while (b.toString().matches(".*\\.[0-9]*0")) {
      if (b.toString().endsWith(".0")) {
        b.setLength(b.length() - 2); // remove ".0"
      } else {
        b.setLength(b.length() - 1); // remove "0"
      }
    }
  }
  return b.toString();
}
 
Example 4
Source Project: calcite   File: RexLiteral.java    License: Apache License 2.0 6 votes vote down vote up
private String intervalString(BigDecimal v) {
  final List<TimeUnit> timeUnits = getTimeUnits(type.getSqlTypeName());
  final StringBuilder b = new StringBuilder();
  for (TimeUnit timeUnit : timeUnits) {
    final BigDecimal[] result = v.divideAndRemainder(timeUnit.multiplier);
    if (b.length() > 0) {
      b.append(timeUnit.separator);
    }
    final int width = b.length() == 0 ? -1 : width(timeUnit); // don't pad 1st
    pad(b, result[0].toString(), width);
    v = result[1];
  }
  if (Util.last(timeUnits) == TimeUnit.MILLISECOND) {
    while (b.toString().matches(".*\\.[0-9]*0")) {
      if (b.toString().endsWith(".0")) {
        b.setLength(b.length() - 2); // remove ".0"
      } else {
        b.setLength(b.length() - 1); // remove "0"
      }
    }
  }
  return b.toString();
}
 
Example 5
Source Project: j2objc   File: BigDecimalArithmeticTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * divideAndRemainder(BigDecimal)
 */
public void testDivideAndRemainder1() {
    String a = "3736186567876876578956958765675671119238118911893939591735";
    int aScale = 45;
    String b = "134432345432345748766876876723342238476237823787879183470";
    int bScale = 70;
    String res = "277923185514690367474770683";
    int resScale = 0;
    String rem = "1.3032693871288309587558885943391070087960319452465789990E-15";
    int remScale = 70;
    BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
    BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
    BigDecimal result[] = aNumber.divideAndRemainder(bNumber);
    assertEquals("incorrect quotient value", res, result[0].toString());
    assertEquals("incorrect quotient scale", resScale, result[0].scale());
    assertEquals("incorrect remainder value", rem, result[1].toString());
    assertEquals("incorrect remainder scale", remScale, result[1].scale());
}
 
Example 6
Source Project: j2objc   File: BigDecimalArithmeticTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * divideAndRemainder(BigDecimal)
 */
public void testDivideAndRemainder2() {
    String a = "3736186567876876578956958765675671119238118911893939591735";
    int aScale = -45;
    String b = "134432345432345748766876876723342238476237823787879183470";
    int bScale = 70;
    String res = "2779231855146903674747706830969461168692256919247547952" +
                 "2608549363170374005512836303475980101168105698072946555" +
                 "6862849";
    int resScale = 0;
    String rem = "3.4935796954060524114470681810486417234751682675102093970E-15";
    int remScale = 70;
    BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
    BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
    BigDecimal result[] = aNumber.divideAndRemainder(bNumber);
    assertEquals("incorrect quotient value", res, result[0].toString());
    assertEquals("incorrect quotient scale", resScale, result[0].scale());
    assertEquals("incorrect remainder value", rem, result[1].toString());
    assertEquals("incorrect remainder scale", remScale, result[1].scale());
}
 
Example 7
Source Project: Bats   File: SqlFunctions.java    License: Apache License 2.0 5 votes vote down vote up
public static BigDecimal floor(BigDecimal b0, BigDecimal b1) {
  final BigDecimal[] bigDecimals = b0.divideAndRemainder(b1);
  BigDecimal r = bigDecimals[1];
  if (r.signum() < 0) {
    r = r.add(b1);
  }
  return b0.subtract(r);
}
 
Example 8
Source Project: Bats   File: SqlFunctions.java    License: Apache License 2.0 5 votes vote down vote up
public static BigDecimal ceil(BigDecimal b0, BigDecimal b1) {
  final BigDecimal[] bigDecimals = b0.divideAndRemainder(b1);
  BigDecimal r = bigDecimals[1];
  if (r.signum() > 0) {
    r = r.subtract(b1);
  }
  return b0.subtract(r);
}
 
Example 9
Source Project: calcite   File: SqlFunctions.java    License: Apache License 2.0 5 votes vote down vote up
public static BigDecimal floor(BigDecimal b0, BigDecimal b1) {
  final BigDecimal[] bigDecimals = b0.divideAndRemainder(b1);
  BigDecimal r = bigDecimals[1];
  if (r.signum() < 0) {
    r = r.add(b1);
  }
  return b0.subtract(r);
}
 
Example 10
/**
 * @param obj
 * @param dbCount
 * @param tbCount
 * @return
 */
public static long getDbIndexByMod(Object obj,int dbCount,int tbCount) {
    long tbRange = getModValue(obj, tbCount);
    BigDecimal bc = new BigDecimal(tbRange);
    BigDecimal[] results = bc.divideAndRemainder(new BigDecimal(dbCount/tbCount));
    return (long)results[0].intValue();
}
 
Example 11
Source Project: Quicksql   File: SqlFunctions.java    License: MIT License 5 votes vote down vote up
public static BigDecimal ceil(BigDecimal b0, BigDecimal b1) {
  final BigDecimal[] bigDecimals = b0.divideAndRemainder(b1);
  BigDecimal r = bigDecimals[1];
  if (r.signum() > 0) {
    r = r.subtract(b1);
  }
  return b0.subtract(r);
}
 
Example 12
Source Project: nebula   File: TickFactory.java    License: Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Round numerator down to multiples of denominators
 *
 * @param numerator
 * @param denominator
 * @return rounded down value
 */
protected static double roundDown(BigDecimal numerator, BigDecimal denominator) {
	final int ns = numerator.signum();
	if (ns == 0)
		return 0;
	final int ds = denominator.signum();
	if (ds == 0) {
		throw new IllegalArgumentException("Zero denominator is not allowed");
	}

	numerator = numerator.abs();
	denominator = denominator.abs();
	final BigDecimal[] x = numerator.divideAndRemainder(denominator);
	double rx = x[1].doubleValue();
	if (rx > (1 - ROUND_FRACTION) * denominator.doubleValue()) {
		// trim up if close to denominator
		x[1] = BigDecimal.ZERO;
		x[0] = x[0].add(BigDecimal.ONE);
	} else if (rx < ROUND_FRACTION * denominator.doubleValue()) {
		x[1] = BigDecimal.ZERO;
	}
	final int xs = x[1].signum();
	if (xs == 0) {
		return ns != ds ? -x[0].multiply(denominator).doubleValue() : x[0].multiply(denominator).doubleValue();
	} else if (xs < 0) {
		throw new IllegalStateException("Cannot happen!");
	}

	if (ns != ds)
		return x[0].signum() == 0 ? -denominator.doubleValue() : -x[0].add(BigDecimal.ONE).multiply(denominator).doubleValue();

	return x[0].multiply(denominator).doubleValue();
}
 
Example 13
Source Project: nebula   File: TickFactory.java    License: Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Round numerator up to multiples of denominators
 *
 * @param numerator
 * @param denominator
 * @return rounded up value
 */
protected static double roundUp(BigDecimal numerator, BigDecimal denominator) {
	final int ns = numerator.signum();
	if (ns == 0)
		return 0;
	final int ds = denominator.signum();
	if (ds == 0) {
		throw new IllegalArgumentException("Zero denominator is not allowed");
	}

	numerator = numerator.abs();
	denominator = denominator.abs();
	final BigDecimal[] x = numerator.divideAndRemainder(denominator);
	double rx = x[1].doubleValue();
	if (rx != 0) {
		if (rx < ROUND_FRACTION * denominator.doubleValue()) {
			// trim down if close to zero
			x[1] = BigDecimal.ZERO;
		} else if (rx > (1 - ROUND_FRACTION) * denominator.doubleValue()) {
			x[1] = BigDecimal.ZERO;
			x[0] = x[0].add(BigDecimal.ONE);
		}
	}
	final int xs = x[1].signum();
	if (xs == 0) {
		return ns != ds ? -x[0].multiply(denominator).doubleValue() : x[0].multiply(denominator).doubleValue();
	} else if (xs < 0) {
		throw new IllegalStateException("Cannot happen!");
	}

	if (ns != ds)
		return x[0].signum() == 0 ? 0 : -x[0].multiply(denominator).doubleValue();

	return x[0].add(BigDecimal.ONE).multiply(denominator).doubleValue();
}
 
Example 14
Source Project: AVM   File: RejectBigDecimalDivision.java    License: MIT License 5 votes vote down vote up
public BigDecimal divide() {
    BigDecimal dividend = new BigDecimal("3.14159");
    BigDecimal divisor = new BigDecimal("1.01");
    MathContext context = MathContext.DECIMAL32;
    BigDecimal pair[] = dividend.divideAndRemainder(divisor, context);
    return pair[0];
}
 
Example 15
Source Project: calcite   File: SqlFunctions.java    License: Apache License 2.0 5 votes vote down vote up
public static BigDecimal ceil(BigDecimal b0, BigDecimal b1) {
  final BigDecimal[] bigDecimals = b0.divideAndRemainder(b1);
  BigDecimal r = bigDecimals[1];
  if (r.signum() > 0) {
    r = r.subtract(b1);
  }
  return b0.subtract(r);
}
 
Example 16
public static final String toBinaryUsingBigDecimal(int numberToConvert) {
    int integer = numberToConvert;
    if (integer<0) throw new IllegalArgumentException("Method argument cannot be negative. number="+integer);
    StringBuilder builder = new StringBuilder();
    BigDecimal number = new BigDecimal(integer);
    BigDecimal[] decimals = null;
    while (number.compareTo(ZERO) > 0) {
        decimals = number.divideAndRemainder(TWO);
        number = decimals[0];
        builder.append(decimals[1]);
    }
    return builder.reverse().toString();
}
 
Example 17
Source Project: Bats   File: SqlFunctions.java    License: Apache License 2.0 4 votes vote down vote up
public static BigDecimal mod(BigDecimal b0, BigDecimal b1) {
  final BigDecimal[] bigDecimals = b0.divideAndRemainder(b1);
  return bigDecimals[1];
}
 
Example 18
Source Project: Quicksql   File: SqlFunctions.java    License: MIT License 4 votes vote down vote up
public static BigDecimal mod(BigDecimal b0, BigDecimal b1) {
  final BigDecimal[] bigDecimals = b0.divideAndRemainder(b1);
  return bigDecimals[1];
}
 
Example 19
Source Project: calcite   File: SqlFunctions.java    License: Apache License 2.0 4 votes vote down vote up
public static BigDecimal mod(BigDecimal b0, BigDecimal b1) {
  final BigDecimal[] bigDecimals = b0.divideAndRemainder(b1);
  return bigDecimals[1];
}
 
Example 20
/**
 *
 * @param obj
 * @param dbCount
 * @param tbCount
 * @return
 */
public static long getTbIndexByMod(Object obj,int dbCount,int tbCount) {
    long tbRange = getModValue(obj, tbCount);
    BigDecimal bc = new BigDecimal(tbRange);
    BigDecimal[] results = bc.divideAndRemainder(new BigDecimal(tbCount/dbCount));
    return (long)results[1].intValue();
}