java - keep x decimal places

public BigDecimal  setScale (int newScale, RoundingMode  roundingMode) 
returns a BigDecimal whose scale is the specified value and whose unscaled value is determined by multiplying or dividing this BigDecimal 's unscaled value by an appropriate power of ten to maintain its total value.
If the operation reduces scale, the unscaled value must be divided (not multiplied), and the value can be changed; in this case, the specified rounding mode is applied to the division.

parameter:
newScale - The scale of the BigDecimal value to return .
roundingMode - The rounding mode to apply.
return:
A BigDecimal whose scale is the specified value and whose unscaled value can be determined by multiplying or dividing this BigDecimal 's unscaled value by an appropriate power of ten to maintain its total value.
One decimal place: ( ROUND_HALF_UP rounding mode)
Double perCent = obj.getNum() * 100.0 / obj.getTotal();
BigDecimal b = new BigDecimal(perCent);
perCent = b.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();

Other modes:

ROUND_DOWN

Rounding mode near zero. Always not increment a number (i.e. truncate) before discarding a portion. Note that this rounding mode never increases the size of the computed value.

ROUND_CEILING

Rounding mode near positive infinity. If BigDecimal is positive, the rounding behavior is the same as ROUND_UP ; if negative, the rounding behavior is the same as ROUND_DOWN . Note that this rounding mode never reduces the calculated value.

ROUND_FLOOR

Rounding mode near negative infinity. If BigDecimal is positive, the rounding behavior is the same as ROUND_DOWN ; if negative, the rounding behavior is the same as ROUND_UP . Note that this rounding mode never adds to the calculated value.

ROUND_HALF_UP

A rounding mode that rounds towards the "nearest" number, if it is equidistant from two adjacent numbers, then rounds up. If the discarded portion is >= 0.5, the rounding behavior is the same as ROUND_UP ; otherwise, the rounding behavior is the same as ROUND_DOWN . Note that this is the rounding mode most of us were taught in elementary school.

ROUND_HALF_DOWN

A rounding mode that rounds towards the "nearest" number, if it is equidistant from two adjacent numbers. If the discarded portion is > 0.5, the rounding behavior is the same as ROUND_UP ; otherwise, the rounding behavior is the same as ROUND_DOWN .

ROUND_HALF_EVEN

Rounds toward the "closest" number, and if it is equidistant from two adjacent numbers, rounds toward the adjacent even number. If the number to the left of the discarded part is odd, the rounding behavior is the same as ROUND_HALF_UP ; if it is even, the rounding behavior is the same as ROUND_HALF_DOWN . Note that this rounding mode minimizes accumulation errors when repeating a series of calculations.

ROUND_UNNECESSARY

Asserts that the requested operation has an exact result, so no rounding is required. Throws an ArithmeticException if this rounding mode is specified for an operation that obtains an exact result .  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326271972&siteId=291194637