use of java.math.MathContext in project robovm by robovm.
the class BigDecimalConstructorsTest method testConstrIntMathContext.
/**
* new BigDecimal(int, MathContext)
*/
public void testConstrIntMathContext() {
int a = 732546982;
int precision = 21;
RoundingMode rm = RoundingMode.CEILING;
MathContext mc = new MathContext(precision, rm);
String res = "732546982";
int resScale = 0;
BigDecimal result = new BigDecimal(a, mc);
assertEquals("incorrect value", res, result.unscaledValue().toString());
assertEquals("incorrect scale", resScale, result.scale());
}
use of java.math.MathContext in project robovm by robovm.
the class BigDecimalConstructorsTest method testConstrCharIntIntMathContext.
/**
* new BigDecimal(char[] value, int offset, int len, MathContext mc);
*/
public void testConstrCharIntIntMathContext() {
char[] value = { '-', '1', '2', '3', '8', '0', '.', '4', '7', '3', '8', 'E', '-', '4', '2', '3' };
int offset = 3;
int len = 12;
int precision = 4;
RoundingMode rm = RoundingMode.CEILING;
MathContext mc = new MathContext(precision, rm);
BigDecimal result = new BigDecimal(value, offset, len, mc);
String res = "3.805E-40";
int resScale = 43;
assertEquals("incorrect value", res, result.toString());
assertEquals("incorrect scale", resScale, result.scale());
try {
// Regression for HARMONY-783
new BigDecimal(new char[] {}, 0, 0, MathContext.DECIMAL32);
fail("NumberFormatException has not been thrown");
} catch (NumberFormatException e) {
}
}
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 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 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;
}
Aggregations