Java Reference
In-Depth Information
static BigDecimal reScale(BigDecimal n,
int scale,
int roundingMode)
{
return (n.setScale(scale, roundingMode));
}
ThismethodwouldreturntheBigDecimaloperandwithanewscale
androundingmode.Forexample:
BigDecimal aNum = new BigDecimal ("3.45");
// Testing setScale() method and rounding mode constants
System.out.println(aNum); // 3.45
aNum = reScale(aNum, 1, BigDecimal.ROUND_UP); // 3.5
System.out.println(aNum);
Sincethescale()methodreturnsthecurrentscaleofaBigDecimal
number,themethodreScale()couldbemodifiedtochangetherounding
modeonly,asfollows:
static BigDecimal reRound(BigDecimal n,
int roundingMode)
{
return (n.setScale(n.scale(), roundingMode));
}
BigDecimal Arithmetic
TheBigDecimalclassprovidesmethodsforperformingbasicarithmeticon
BigDecimalnumbers. Table22-2 i sasummaryofthesemethods.
Table 22-2
Arithmetic Methods in BigDecimal Class
Abs()
Returns a BigDecimal whose value is the absolute value
of this BigDecimal()
add(BigDecimal val)
Returns a BigDecimal whose value is the sum of the
argment and the operand
divide(BigDecimal val, int roundingMode)
Returns a BigDecimal whose value is the quotient of the
argument and the operand
divide(BigDecimal val, int scale, int roundingMode)
Returns a BigDecimal whose value is the quotient of the
argument and the operand and whose scale is as
specified
max(BigDecimal val)
Returns the maximum of this BigDecimal and val.
min(BigDecimal val)
Returns the minimum of this BigDecimal and val
(continues)
 
Search WWH ::




Custom Search