/** * Produces a value half of a "step" back in this expression's rounding scale. * For example with a scale of 2, "2.5" would be stepped back to "2.495". */ protected final BigDecimal halfStepPrevInScale(BigDecimal decimal) { BigDecimal step = BigDecimal.ONE.scaleByPowerOfTen(-getRoundingScale()); BigDecimal halfStep = step.divide(BigDecimal.valueOf(2)); return decimal.subtract(halfStep); }
/** * Produces a value one "step" forward in this expression's rounding scale. * For example with a scale of 2, "2.5" would be stepped forward to "2.51". */ protected final BigDecimal stepNextInScale(BigDecimal decimal) { BigDecimal step = BigDecimal.ONE.scaleByPowerOfTen(-getRoundingScale()); return decimal.add(step); }
/** * Produces a value half of a "step" forward in this expression's rounding scale. * For example with a scale of 2, "2.5" would be stepped forward to "2.505". */ protected final BigDecimal halfStepNextInScale(BigDecimal decimal) { BigDecimal step = BigDecimal.ONE.scaleByPowerOfTen(-getRoundingScale()); BigDecimal halfStep = step.divide(BigDecimal.valueOf(2)); return decimal.add(halfStep); }
/** * Returns the given decimal rounded to this rounding expression's scale. * For example, with scale 2 the decimal "2.453" would be rounded to either 2.45 or * 2.46 depending on the rounding mode, while "2.38" and "2.7" would be unchanged. * @param decimal the decimal to round * @return the rounded result decimal */ protected final BigDecimal roundToScale(BigDecimal decimal) { return decimal.setScale(getRoundingScale(), getRoundingMode()); }
/** * Produces a value one "step" back in this expression's rounding scale. * For example with a scale of 2, "2.5" would be stepped back to "2.49". */ protected final BigDecimal stepPrevInScale(BigDecimal decimal) { BigDecimal step = BigDecimal.ONE.scaleByPowerOfTen(-getRoundingScale()); return decimal.subtract(step); }
/** * Finds the Decimal KeyRange that will produce the given result when fed into this * rounding expression. For example, a ROUND expression with scale 2 will produce the * result "2.05" with any decimal in the range [2.045, 2.0545). * The result must be pre-rounded to within this rounding expression's scale. * @param result the result to find an input range for. Must be producable. * @return a KeyRange of DECIMAL keys that can be rounded by this expression to produce result * @throws IllegalArgumentException if the result has more scale than this expression can produce */ protected KeyRange getInputRangeProducing(BigDecimal result) { if(!hasEnoughPrecisionToProduce(result)) { throw new IllegalArgumentException("Cannot produce input range for decimal " + result + ", not enough precision with scale " + getRoundingScale()); } byte[] lowerRange = PDecimal.INSTANCE.toBytes(halfStepPrevInScale(result)); byte[] upperRange = PDecimal.INSTANCE.toBytes(halfStepNextInScale(result)); // inclusiveness changes depending on sign // e.g. -0.5 rounds "up" to -1 even though it is the lower boundary boolean lowerInclusive = result.signum() > 0; boolean upperInclusive = result.signum() < 0; return KeyRange.getKeyRange(lowerRange, lowerInclusive, upperRange, upperInclusive); }
/** * Returns the given decimal rounded to this rounding expression's scale. * For example, with scale 2 the decimal "2.453" would be rounded to either 2.45 or * 2.46 depending on the rounding mode, while "2.38" and "2.7" would be unchanged. * @param decimal the decimal to round * @return the rounded result decimal */ protected final BigDecimal roundToScale(BigDecimal decimal) { return decimal.setScale(getRoundingScale(), getRoundingMode()); }
/** * Produces a value one "step" back in this expression's rounding scale. * For example with a scale of 2, "2.5" would be stepped back to "2.49". */ protected final BigDecimal stepPrevInScale(BigDecimal decimal) { BigDecimal step = BigDecimal.ONE.scaleByPowerOfTen(-getRoundingScale()); return decimal.subtract(step); }
/** * Produces a value one "step" back in this expression's rounding scale. * For example with a scale of 2, "2.5" would be stepped back to "2.49". */ protected final BigDecimal stepPrevInScale(BigDecimal decimal) { BigDecimal step = BigDecimal.ONE.scaleByPowerOfTen(-getRoundingScale()); return decimal.subtract(step); }
/** * Produces a value half of a "step" back in this expression's rounding scale. * For example with a scale of 2, "2.5" would be stepped back to "2.495". */ protected final BigDecimal halfStepPrevInScale(BigDecimal decimal) { BigDecimal step = BigDecimal.ONE.scaleByPowerOfTen(-getRoundingScale()); BigDecimal halfStep = step.divide(BigDecimal.valueOf(2)); return decimal.subtract(halfStep); }
/** * Produces a value one "step" forward in this expression's rounding scale. * For example with a scale of 2, "2.5" would be stepped forward to "2.51". */ protected final BigDecimal stepNextInScale(BigDecimal decimal) { BigDecimal step = BigDecimal.ONE.scaleByPowerOfTen(-getRoundingScale()); return decimal.add(step); }
/** * Produces a value half of a "step" back in this expression's rounding scale. * For example with a scale of 2, "2.5" would be stepped back to "2.495". */ protected final BigDecimal halfStepPrevInScale(BigDecimal decimal) { BigDecimal step = BigDecimal.ONE.scaleByPowerOfTen(-getRoundingScale()); BigDecimal halfStep = step.divide(BigDecimal.valueOf(2)); return decimal.subtract(halfStep); }
/** * Produces a value half of a "step" forward in this expression's rounding scale. * For example with a scale of 2, "2.5" would be stepped forward to "2.505". */ protected final BigDecimal halfStepNextInScale(BigDecimal decimal) { BigDecimal step = BigDecimal.ONE.scaleByPowerOfTen(-getRoundingScale()); BigDecimal halfStep = step.divide(BigDecimal.valueOf(2)); return decimal.add(halfStep); }
/** * Produces a value one "step" forward in this expression's rounding scale. * For example with a scale of 2, "2.5" would be stepped forward to "2.51". */ protected final BigDecimal stepNextInScale(BigDecimal decimal) { BigDecimal step = BigDecimal.ONE.scaleByPowerOfTen(-getRoundingScale()); return decimal.add(step); }
/** * Returns the given decimal rounded to this rounding expression's scale. * For example, with scale 2 the decimal "2.453" would be rounded to either 2.45 or * 2.46 depending on the rounding mode, while "2.38" and "2.7" would be unchanged. * @param decimal the decimal to round * @return the rounded result decimal */ protected final BigDecimal roundToScale(BigDecimal decimal) { return decimal.setScale(getRoundingScale(), getRoundingMode()); }
/** * Produces a value half of a "step" forward in this expression's rounding scale. * For example with a scale of 2, "2.5" would be stepped forward to "2.505". */ protected final BigDecimal halfStepNextInScale(BigDecimal decimal) { BigDecimal step = BigDecimal.ONE.scaleByPowerOfTen(-getRoundingScale()); BigDecimal halfStep = step.divide(BigDecimal.valueOf(2)); return decimal.add(halfStep); }
/** * Finds the Decimal KeyRange that will produce the given result when fed into this * rounding expression. For example, a ROUND expression with scale 2 will produce the * result "2.05" with any decimal in the range [2.045, 2.0545). * The result must be pre-rounded to within this rounding expression's scale. * @param result the result to find an input range for. Must be producable. * @return a KeyRange of DECIMAL keys that can be rounded by this expression to produce result * @throws IllegalArgumentException if the result has more scale than this expression can produce */ protected KeyRange getInputRangeProducing(BigDecimal result) { if(!hasEnoughPrecisionToProduce(result)) { throw new IllegalArgumentException("Cannot produce input range for decimal " + result + ", not enough precision with scale " + getRoundingScale()); } byte[] lowerRange = PDecimal.INSTANCE.toBytes(halfStepPrevInScale(result)); byte[] upperRange = PDecimal.INSTANCE.toBytes(halfStepNextInScale(result)); // inclusiveness changes depending on sign // e.g. -0.5 rounds "up" to -1 even though it is the lower boundary boolean lowerInclusive = result.signum() > 0; boolean upperInclusive = result.signum() < 0; return KeyRange.getKeyRange(lowerRange, lowerInclusive, upperRange, upperInclusive); }
/** * Finds the Decimal KeyRange that will produce the given result when fed into this * rounding expression. For example, a ROUND expression with scale 2 will produce the * result "2.05" with any decimal in the range [2.045, 2.0545). * The result must be pre-rounded to within this rounding expression's scale. * @param result the result to find an input range for. Must be producable. * @return a KeyRange of DECIMAL keys that can be rounded by this expression to produce result * @throws IllegalArgumentException if the result has more scale than this expression can produce */ protected KeyRange getInputRangeProducing(BigDecimal result) { if(!hasEnoughPrecisionToProduce(result)) { throw new IllegalArgumentException("Cannot produce input range for decimal " + result + ", not enough precision with scale " + getRoundingScale()); } byte[] lowerRange = PDecimal.INSTANCE.toBytes(halfStepPrevInScale(result)); byte[] upperRange = PDecimal.INSTANCE.toBytes(halfStepNextInScale(result)); // inclusiveness changes depending on sign // e.g. -0.5 rounds "up" to -1 even though it is the lower boundary boolean lowerInclusive = result.signum() > 0; boolean upperInclusive = result.signum() < 0; return KeyRange.getKeyRange(lowerRange, lowerInclusive, upperRange, upperInclusive); }