You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fractal edited this page Sep 13, 2010
·
3 revisions
The Decimal object is a core member of Quantify.
It simplifies operations with arbitrary precision reals (BigDecimal). It provides a very concise API through the use of static factories named $. This significantly reduces the amount of code necessary.
example:
@Test
public void testShowASimpleChainingVersusBigDecimal(){
BigDecimal result = new BigDecimal(125.75).subtract(new BigDecimal(100)).add(new BigDecimal(4.25)).divide(new BigDecimal(3), new MathContext(2));
assertEquals(TEN, result);
//versus
assertDecimal(10, $(125.75).minus(100).plus(4.25).divide(3));
}
What would you rather?
Let’s see how it simplifies Divisions, which, if not familiar with, can be quite tricky. Both of them produce 45:
@Test
public void testDivisionRoundedToInt(){
BigDecimal expected = new BigDecimal(312d).divide(new BigDecimal(7d), RoundingMode.HALF_EVEN);
assertEquals(new BigDecimal(45), expected);
assertDecimal(45, new Divide(312, 7).halfEven().eval());
}
or this simpler example:
@Test
public void testCompareDivisionWithDefaultPrecision(){
BigDecimal expected = new BigDecimal(312d).divide(new BigDecimal(7d), 10, RoundingMode.HALF_EVEN);
assertEquals(new BigDecimal(44.5714285714, new MathContext(12)), expected);
assertDecimal(44.5714285714, $(312).divide(7));
}