Original Address: https: //www.jianshu.com/p/2947868d76eb
Scenarios
Most business computing, the general class java.math.BigDecimal accurate calculation. For example: Money
use
1, constructed BigDecimal
BigDecimal BigDecimal(double d); //不允许使用,精度不能保证 BigDecimal BigDecimal(String s); //常用,推荐使用 static BigDecimal valueOf(double d); //常用,推荐使用
2, method
method | Miao Xu |
---|---|
add(BigDecimal) | BigDecimal object values are added, and then return the object |
subtract(BigDecimal) | Value BigDecimal object subtraction, and return the object |
multiply(BigDecimal) | BigDecimal object value is multiplied, and then return the object |
divide(BigDecimal) | BigDecimal object divided by the value, and then return the object |
toString() | BigDecimal to convert the value into a string object |
doubleValue() | The value returned BigDecimal object to double the number of |
floatValue () | The value returned BigDecimal object to single precision |
long value () | The value returned BigDecimal object to a long integer |
intValue() | The value returned BigDecimal object to an integer. |
3, formatting and rounding
// 格式化:保留2为小数
DecimalFormat df = new DecimalFormat("#.##");
// 四舍五入,默认五舍六入
df.setRoundingMode(RoundingMode.HALF_UP);
4, formatting
DecimalFormat analysis:
symbol | position | Miao Xu |
---|---|---|
0 | digital | Arabic numerals, if there is displayed 0 |
# | digital | Arabic numerals, if there is no display 0 |
. | digital | Decimal separator or monetary decimal separator |
, | digital | Packet delimiter |
E | digital | Partition mantissa and exponent in scientific notation. In the prefix or suffix Need not be quoted. |
- | digital | negative |
; | Sub-mode boundary | Partition positive and negative subpatterns |
% | Prefix or suffix | Multiplied by 100 and is displayed as a percentage |
\u2030 | Prefix or suffix | Multiply by 1000 and show as per mille |
¤ (\ U00a4) | Prefix or suffix | Currency sign, replaced by currency symbol. If both occur simultaneously, replaced by international currency symbol. If present in a pattern, the monetary decimal separators, instead of the decimal separator. |
' | Prefix or suffix | A prefix or suffix to quote special characters, such as " '#' #" formats 123 "# 123." To create a single quote itself, use two in single quotation marks: "# o''clock". |
5, the rounding mode Introduction
-
RoundingMode.CEILNG
: The general direction toward positive infinity rounding rounding mode. If the result is positive, the rounding behavior is similarRoundingMode.UP
; if the result is negative, the rounding behavior is similar toRoundingMode.DOWN
the following:
5.5 => 6
1.1 => 2
-1.0 => -1 -2.5 => -2
-
RoundingMode.DOWN
: Towards zero rounding mode rounding. Never dropped off the front of the number plus 1 (i.e. censored). Note that this rounding mode never increases the calculated value of the absolute values
are as follows:
5.5 => 5
1.1 => 1
-1.0 => -1 -1.6 => -1
-
RoundingMode.FLOOR
: The general direction toward negative infinity rounding rounding mode. If the result is positive, the rounding behavior is similar to RoundingMode.DOWN; if the result is negative, the rounding behaves like RoundingMode.UP
注意:此舍入模式始终不会增加计算值
as follows:
5.5 => 5
2.3 => 2
1.0 => 1 -1.1 => -2 -2.5 => -3
- RoundingMode.HALF_DOWN: the closest to the direction of rounding numbers rounding mode, if the numbers are equal to the distance of two adjacent, rounded down. If the discarded fraction is> 0.5, the rounding behavior with RoundingMode.UP; otherwise the same rounding behavior RoundingMode.DOWN;
五舍六入
follows:
2.5 => 2
1.6 => 2
-1.1 => -1 -1.6 => -2 -2.5 => -2
- RoundingMode.HALF_EVEN: the closest to the direction of rounding numbers rounding mode, if the numbers are equal distance from two adjacent, the adjacent even to rounding. If the number is odd part left of the discarded, the rounding behavior with RoundingMode.HALF_UP; if even, then rounding behavior with RoundingMode.HALF_DOWN. Note that when repeating a series of calculations, the rounding mode that minimizes cumulative error in the statistics on. This rounding mode is also known as the "banker's rounding method", mainly in the United States. This rounding mode is similar to the Java float and double strategy rounding algorithm used
is as follows:
5.5 => 6
2.5 => 2
1.1 => 1 -1.0 => -1 -1.6 => -2 -2.5 => -2
- RoundingMode.HALF_UP: the closest to the direction of rounding numbers rounding mode, if the numbers are equal to the distance of two adjacent, rounded up. If the discarded fraction is> = 0.5, the rounding behavior with RoundingMode.UP; otherwise RoundingMode.DOWN rounding behavior with
注意:此舍入模式就是通常学校里讲的四舍五入
the following:
2.5 => 3
1.1 => 1
-1.1 => -1 -1.6 => -2
- RoundingMode.UNNECESSARY: for asserting the requested operation with accurate results rounding mode, hence no rounding. If the designated rounding mode to generate precise operation result is thrown ArithmeticException
follows:
1.5 => 抛出 ArithmeticException
1.1 => 抛出 ArithmeticException
1.0 => 1
-1.1 =>抛出 ArithmeticException -1.6 => 抛出 ArithmeticException
- RoundingMode.UP: rounding away from zero rounding mode. Always in front of the digital non-zero discarded fraction plus one. Note that this rounding mode does not reduce the absolute value calculated
as follows:
5.5 => 6
1.1 => 2
-1.1 => -2 -1.6 => -2
problem
- When BigDecimal divide method is not divisible when dividing, it occurs when the infinite decimal, it will throw an abnormality, the abnormality as follows: java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result at java.math.BigDecimal. .divide (Unknown Source)
Solution: Set accurate to divide the decimal point