use of java.math.MathContext in project groovy by apache.
the class BigDecimalMath method divideImpl.
public Number divideImpl(Number left, Number right) {
BigDecimal bigLeft = toBigDecimal(left);
BigDecimal bigRight = toBigDecimal(right);
try {
return bigLeft.divide(bigRight);
} catch (ArithmeticException e) {
// set a DEFAULT precision if otherwise non-terminating
int precision = Math.max(bigLeft.precision(), bigRight.precision()) + DIVISION_EXTRA_PRECISION;
BigDecimal result = bigLeft.divide(bigRight, new MathContext(precision));
int scale = Math.max(Math.max(bigLeft.scale(), bigRight.scale()), DIVISION_MIN_SCALE);
if (result.scale() > scale)
result = result.setScale(scale, BigDecimal.ROUND_HALF_UP);
return result;
}
}
use of java.math.MathContext in project head by mifos.
the class Money method round.
/**
* This method returns a new Money object with currency same as current
* currency and amount calculated after rounding based on rounding mode and
* roundingAmount where in both are obtained from MifosCurrency object. <br />
* <br />
* The rounding calculation is as follows:- Lets say we want to round 142.34
* to nearest 50 cents and and rounding mode is ceil (i.e. to greater
* number) we will divide 142.34 by .5 which will result in 284.68 now we
* will round this to a whole number using ceil mode which will result in
* 285 and then multiply 285 by 0.5 resulting in 142.5.
*
*/
public static Money round(Money money, BigDecimal roundOffMultiple, RoundingMode roundingMode) {
// insure that we are using the correct internal precision
BigDecimal roundingAmount = roundOffMultiple.setScale(internalPrecision, internalRoundingMode);
// FIXME: are we loosing precision here
// mathcontext only take cares of significant digits
// not digit right to the decimal
BigDecimal nearestFactor = money.getAmount().divide(roundingAmount, new MathContext(internalPrecision, internalRoundingMode));
nearestFactor = nearestFactor.setScale(0, roundingMode);
BigDecimal roundedAmount = nearestFactor.multiply(roundingAmount);
return new Money(money.getCurrency(), roundedAmount);
}
use of java.math.MathContext in project openhab1-addons by openhab.
the class PlexConnector method getProgressCommand.
private String getProgressCommand(PlexBindingConfig config, Command command) {
PlexSession session = getSessionByMachineId(config.getMachineIdentifier());
String url = null;
if (session != null) {
int offset = 0;
if (command.getClass().equals(PercentType.class)) {
PercentType percent = (PercentType) command;
offset = new BigDecimal(session.getDuration()).multiply(percent.toBigDecimal().divide(new BigDecimal("100"), new MathContext(5, RoundingMode.HALF_UP))).intValue();
offset = Math.max(0, offset);
offset = Math.min(session.getDuration(), offset);
url = String.format("playback/seekTo?offset=%d", offset);
} else if (command.getClass().equals(IncreaseDecreaseType.class)) {
if (command.equals(IncreaseDecreaseType.DECREASE)) {
url = PlexProperty.STEP_BACK.getName();
} else {
url = PlexProperty.STEP_FORWARD.getName();
}
}
}
return url;
}
use of java.math.MathContext in project openhab1-addons by openhab.
the class PlexSession method updateProgress.
private void updateProgress() {
if (duration > 0) {
BigDecimal progress = new BigDecimal("100").divide(new BigDecimal(duration), new MathContext(100, RoundingMode.HALF_UP)).multiply(new BigDecimal(viewOffset)).setScale(2, RoundingMode.HALF_UP);
progress = BigDecimal.ZERO.max(progress);
progress = new BigDecimal("100").min(progress);
this.progress = progress;
this.endTime = new Date(System.currentTimeMillis() + (duration - viewOffset));
}
}
use of java.math.MathContext in project robovm by robovm.
the class BigDecimalConstructorsTest method testConstrLongMathContext.
/**
* new BigDecimal(long, MathContext)
*/
public void testConstrLongMathContext() {
long a = 4576578677732546982L;
int precision = 5;
RoundingMode rm = RoundingMode.CEILING;
MathContext mc = new MathContext(precision, rm);
String res = "45766";
int resScale = -14;
BigDecimal result = new BigDecimal(a, mc);
assertEquals("incorrect value", res, result.unscaledValue().toString());
assertEquals("incorrect scale", resScale, result.scale());
}
Aggregations