BigDecimal.setScale handles java decimal points

http://blog.csdn.net/ahwr24/article/details/7048724

The BigDecimal.setScale() method is used to format the decimal point
setScale(1) means that one decimal place is reserved. By default, the rounding method 
setScale(1, BigDecimal.ROUND_DOWN) is used to directly delete the extra decimal places. For example, 2.35 will become 2.3 
setScale(1, BigDecimal.ROUND_UP) carry processing, 2.35 becomes 2.4 
setScale(1, BigDecimal.ROUND_HALF_UP) rounds, 2.35 becomes 2.4
setScaler(1, BigDecimal.ROUND_HALF_DOWN) rounds up, 2.35 becomes 2.3, if it is 5, it rounds down
 
 
Notes:
1:
scale refers to the number of digits after your decimal point. For example, 123.456 score is 3.
score() is a method in the BigDecimal class.
For example: BigDecimal b = new BigDecimal("123.456");
b.scale(), which returns 3.
2:
roundingMode is a reserved mode for decimals. They are all constant fields in BigDecimal, there are many kinds.
For example: BigDecimal.ROUND_HALF_UP means that 4 is rounded to 5.
3:
pubilc BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)
means: I divide the result of a BigDecimal object by the divisor, and require this result to retain scale decimal places, roundingMode means that the retention mode is What, rounding or something else, you can choose!

4 : For the general add, subtract and multiply methods, the decimal places are formatted as follows:

BigDecimal mData = new BigDecimal("9.655").setScale(2, BigDecimal.ROUND_HALF_UP);
        System.out.println("mData=" + mData);
 
----Result:----- mData=9.66

Guess you like

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