前言
在些银行或商城项目业务中,需要数据的精度要求较高,而在Java中基本数据类型‘float’或者‘double’这些代表浮点型的数据类型在进行数据运输时结果并不会和我们预期的一致,因为其运算可能会发生精度丢失。
在这样引用一个比较经典的案例:为什么会出现4.0-3.6=0.3999999999这种现象?
double a = 4.0;
double b = 3.6;
System.out.println(a-b); //结果:0.3999999999999999
这种舍入误差的主要原因是浮点数值采用二进制系统表示, 而在二进制系统中无法精确地表示分数 1/10。这就好像十进制无法精确地表示分数 1/3—样。针对十进制,1除以3是除不尽的。很好理解,因为我们一直接触的就是十进制,等于0.333333… 很好理解。但是:二进制系统中无法精确地表示分数 1/10。为啥呢。就有点不理解了?
上图为《java核心技术》一书中的解释,其中重点在于‘主要原因是浮点数值采用二进制系统’这句话。拿上面案例举例,计算过程为:
- 将十进制的 4.0 转换成 二进制,将十进制的 3.6 转换成二进制;
- 使用转换后的二进制,进行减法运算
而3.6转成二进制则是11.100110011001…(一直循环除不尽)
进制转换参考:https://www.runoob.com/w3cnote/decimal-decimals-are-converted-to-binary-fractions.html
一、BigDecimal
1.1 概述
Java在java.math包中提供的API类BigDecimal,用来对超过16位有效位的数进行精确的运算。双精度浮点型变量double可以处理16位有效数,但在实际应用中,可能需要对更大或者更小的数进行运算和处理。
一般情况下,对于那些不需要准确计算精度的数字,我们可以直接使用Float和Double处理,但是Double.valueOf(String) 和Float.valueOf(String)会丢失精度。所以开发中,如果我们需要精确计算的结果,则必须使用BigDecimal类来操作。
BigDecimal所创建的是对象,故我们不能使用传统的+、-、*、/等算术运算符直接对其对象进行数学运算,而必须调用其相对应的方法。方法中的参数也必须是BigDecimal的对象。构造器是类的特殊方法,专门用来创建对象,特别是带有参数的对象。
1.2 常用构造函数
参考API:https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html
使用注意:尽量使用参数类型为String的构造函数。
BigDecimal a =new BigDecimal(0.1);
System.out.println("a values is:"+a);
System.out.println("=====================");
BigDecimal b =new BigDecimal("0.1");
System.out.println("b values is:"+b);
a values is:0.1000000000000000055511151231257827021181583404541015625
=====================
b values is:0.1
1.3 常用方法
-
add(BigDecimal)
BigDecimal对象中的值相加,返回BigDecimal对象 -
subtract(BigDecimal)
BigDecimal对象中的值相减,返回BigDecimal对象 -
multiply(BigDecimal)
BigDecimal对象中的值相乘,返回BigDecimal对象 -
divide(BigDecimal)
BigDecimal对象中的值相除,返回BigDecimal对象 -
toString()
将BigDecimal对象中的值转换成字符串 -
doubleValue()
将BigDecimal对象中的值转换成双精度数 -
floatValue()
将BigDecimal对象中的值转换成单精度数 -
longValue()
将BigDecimal对象中的值转换成长整数 -
intValue()
将BigDecimal对象中的值转换成整数
1.4 BigDecimal比较大小
java中对BigDecimal比较大小一般用的是bigdemical的compareTo方法
int a = bigdemical.compareTo(bigdemical2)
返回结果分析:
a = -1,表示bigdemical小于bigdemical2;
a = 0,表示bigdemical等于bigdemical2;
a = 1,表示bigdemical大于bigdemical2;
举例:a大于等于b
new bigdemica(a).compareTo(new bigdemical(b)) >= 0
二、DecimalFormat
开发过程中,通常会遇到一个问题:我们需要将一个数值转换为格式化的数值,比:3.145678保留两位有效数字,这时候我们该用什么方法来转换呢?
就可以使用DecimalFormat进行修饰,类似与SimpleDateFormat。
JavaSE官方文档的描述
- DecimalFormat 是 NumberFormat 的一个具体子类,用于格式化十进制数字。该类设计有各种功能,使其能够解析和格式化任意语言环境中的数,包括对西方语言、阿拉伯语和印度语数字的支持。它还支持不同类型的数,包括整数 (123)、定点数 (123.4)、科学记数法表示的数 (1.23E4)、百分数 (12%) 和金额 ($123)。所有这些内容都可以本地化。
- 舍入: DecimalFormat 提供 RoundingMode 中定义的舍入模式进行格式化。默认情况下,它使用 RoundingMode.HALF_EVEN
构造函数:
public DecimalFormat(String pattern) {
// Always applyPattern after the symbols are set
this.symbols = DecimalFormatSymbols.getInstance(Locale.getDefault(Locale.Category.FORMAT));
applyPattern(pattern, false);
}
而上面构造函数的入参pattern通常会使用占位符,‘#’和‘0’。
public class DecimalDemo {
public static void main(String[] args) {
System.out.println(format("00.000",new BigDecimal("3.14"))); // 03.140,占位符多余用0填补
System.out.println(format("0.0",new BigDecimal("31.14"))); // 31.1 整数保留
System.out.println(format("0.0",new BigDecimal("31.16"))); // 31.2 整数保留,小数四舍五入
System.out.println(format("##.###",new BigDecimal("3.14"))); // 3.14,占位符 多余舍弃
System.out.println(format("#.#",new BigDecimal("31.14"))); // 31.1 整数保留
System.out.println(format("#.#",new BigDecimal("31.16"))); // 31.2 整数保留,小数四舍五入
}
public static String format(String pattern, BigDecimal bigDecimal){
DecimalFormat decimalFormat = new DecimalFormat(pattern);
return decimalFormat.format(bigDecimal);
}
}
总结:
使用0占位符的时候:
1、 占位符比实际数字的位数多,不足的地方用0补上。
2、 占位符比实际数字的位数少:整数部分不改动,小数部分,四舍五入(其实并不是四舍五入,而是默认的RoundingMode.HALF_EVEN方式)。
使用#占位符的时候:
1、 比实际数字的位数多,不变。
2、 比实际数字的位数少:整数部分不改动,小数部分,四舍五入(其实并不是四舍五入,而是默认的RoundingMode.HALF_EVEN方式)。
DecimalFormat舍入方式:
当要格式化的数字超过占位符的时候,格式化的结果会进行四舍五入。这并不是按照四舍五入的方式舍入的,而是因为没有指定格式化的RoundingMode,而默认使用了RoundingMode.HALF_EVEN方式。
package java.math;
/**
* @see BigDecimal
* @see MathContext
* @author Josh Bloch
* @author Mike Cowlishaw
* @author Joseph D. Darcy
* @since 1.5
*/
public enum RoundingMode {
/**
* Rounding mode to round away from zero. Always increments the
* digit prior to a non-zero discarded fraction. Note that this
* rounding mode never decreases the magnitude of the calculated
* value.
*
*<p>Example:
*<table border>
* <caption><b>Rounding mode UP Examples</b></caption>
*<tr valign=top><th>Input Number</th>
* <th>Input rounded to one digit<br> with {@code UP} rounding
*<tr align=right><td>5.5</td> <td>6</td>
*<tr align=right><td>2.5</td> <td>3</td>
*<tr align=right><td>1.6</td> <td>2</td>
*<tr align=right><td>1.1</td> <td>2</td>
*<tr align=right><td>1.0</td> <td>1</td>
*<tr align=right><td>-1.0</td> <td>-1</td>
*<tr align=right><td>-1.1</td> <td>-2</td>
*<tr align=right><td>-1.6</td> <td>-2</td>
*<tr align=right><td>-2.5</td> <td>-3</td>
*<tr align=right><td>-5.5</td> <td>-6</td>
*</table>
*/
UP(BigDecimal.ROUND_UP),
/**
* Rounding mode to round towards zero. Never increments the digit
* prior to a discarded fraction (i.e., truncates). Note that this
* rounding mode never increases the magnitude of the calculated value.
*
*<p>Example:
*<table border>
* <caption><b>Rounding mode DOWN Examples</b></caption>
*<tr valign=top><th>Input Number</th>
* <th>Input rounded to one digit<br> with {@code DOWN} rounding
*<tr align=right><td>5.5</td> <td>5</td>
*<tr align=right><td>2.5</td> <td>2</td>
*<tr align=right><td>1.6</td> <td>1</td>
*<tr align=right><td>1.1</td> <td>1</td>
*<tr align=right><td>1.0</td> <td>1</td>
*<tr align=right><td>-1.0</td> <td>-1</td>
*<tr align=right><td>-1.1</td> <td>-1</td>
*<tr align=right><td>-1.6</td> <td>-1</td>
*<tr align=right><td>-2.5</td> <td>-2</td>
*<tr align=right><td>-5.5</td> <td>-5</td>
*</table>
*/
DOWN(BigDecimal.ROUND_DOWN),
/**
* Rounding mode to round towards positive infinity. If the
* result is positive, behaves as for {@code RoundingMode.UP};
* if negative, behaves as for {@code RoundingMode.DOWN}. Note
* that this rounding mode never decreases the calculated value.
*
*<p>Example:
*<table border>
* <caption><b>Rounding mode CEILING Examples</b></caption>
*<tr valign=top><th>Input Number</th>
* <th>Input rounded to one digit<br> with {@code CEILING} rounding
*<tr align=right><td>5.5</td> <td>6</td>
*<tr align=right><td>2.5</td> <td>3</td>
*<tr align=right><td>1.6</td> <td>2</td>
*<tr align=right><td>1.1</td> <td>2</td>
*<tr align=right><td>1.0</td> <td>1</td>
*<tr align=right><td>-1.0</td> <td>-1</td>
*<tr align=right><td>-1.1</td> <td>-1</td>
*<tr align=right><td>-1.6</td> <td>-1</td>
*<tr align=right><td>-2.5</td> <td>-2</td>
*<tr align=right><td>-5.5</td> <td>-5</td>
*</table>
*/
CEILING(BigDecimal.ROUND_CEILING),
/**
* Rounding mode to round towards negative infinity. If the
* result is positive, behave as for {@code RoundingMode.DOWN};
* if negative, behave as for {@code RoundingMode.UP}. Note that
* this rounding mode never increases the calculated value.
*
*<p>Example:
*<table border>
* <caption><b>Rounding mode FLOOR Examples</b></caption>
*<tr valign=top><th>Input Number</th>
* <th>Input rounded to one digit<br> with {@code FLOOR} rounding
*<tr align=right><td>5.5</td> <td>5</td>
*<tr align=right><td>2.5</td> <td>2</td>
*<tr align=right><td>1.6</td> <td>1</td>
*<tr align=right><td>1.1</td> <td>1</td>
*<tr align=right><td>1.0</td> <td>1</td>
*<tr align=right><td>-1.0</td> <td>-1</td>
*<tr align=right><td>-1.1</td> <td>-2</td>
*<tr align=right><td>-1.6</td> <td>-2</td>
*<tr align=right><td>-2.5</td> <td>-3</td>
*<tr align=right><td>-5.5</td> <td>-6</td>
*</table>
*/
FLOOR(BigDecimal.ROUND_FLOOR),
/**
* Rounding mode to round towards {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case round up.
* Behaves as for {@code RoundingMode.UP} if the discarded
* fraction is ≥ 0.5; otherwise, behaves as for
* {@code RoundingMode.DOWN}. Note that this is the rounding
* mode commonly taught at school.
*
*<p>Example:
*<table border>
* <caption><b>Rounding mode HALF_UP Examples</b></caption>
*<tr valign=top><th>Input Number</th>
* <th>Input rounded to one digit<br> with {@code HALF_UP} rounding
*<tr align=right><td>5.5</td> <td>6</td>
*<tr align=right><td>2.5</td> <td>3</td>
*<tr align=right><td>1.6</td> <td>2</td>
*<tr align=right><td>1.1</td> <td>1</td>
*<tr align=right><td>1.0</td> <td>1</td>
*<tr align=right><td>-1.0</td> <td>-1</td>
*<tr align=right><td>-1.1</td> <td>-1</td>
*<tr align=right><td>-1.6</td> <td>-2</td>
*<tr align=right><td>-2.5</td> <td>-3</td>
*<tr align=right><td>-5.5</td> <td>-6</td>
*</table>
*/
HALF_UP(BigDecimal.ROUND_HALF_UP),
/**
* Rounding mode to round towards {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case round
* down. Behaves as for {@code RoundingMode.UP} if the discarded
* fraction is > 0.5; otherwise, behaves as for
* {@code RoundingMode.DOWN}.
*
*<p>Example:
*<table border>
* <caption><b>Rounding mode HALF_DOWN Examples</b></caption>
*<tr valign=top><th>Input Number</th>
* <th>Input rounded to one digit<br> with {@code HALF_DOWN} rounding
*<tr align=right><td>5.5</td> <td>5</td>
*<tr align=right><td>2.5</td> <td>2</td>
*<tr align=right><td>1.6</td> <td>2</td>
*<tr align=right><td>1.1</td> <td>1</td>
*<tr align=right><td>1.0</td> <td>1</td>
*<tr align=right><td>-1.0</td> <td>-1</td>
*<tr align=right><td>-1.1</td> <td>-1</td>
*<tr align=right><td>-1.6</td> <td>-2</td>
*<tr align=right><td>-2.5</td> <td>-2</td>
*<tr align=right><td>-5.5</td> <td>-5</td>
*</table>
*/
HALF_DOWN(BigDecimal.ROUND_HALF_DOWN),
/**
* Rounding mode to round towards the {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case, round
* towards the even neighbor. Behaves as for
* {@code RoundingMode.HALF_UP} if the digit to the left of the
* discarded fraction is odd; behaves as for
* {@code RoundingMode.HALF_DOWN} if it's even. Note that this
* is the rounding mode that statistically minimizes cumulative
* error when applied repeatedly over a sequence of calculations.
* It is sometimes known as {@literal "Banker's rounding,"} and is
* chiefly used in the USA. This rounding mode is analogous to
* the rounding policy used for {@code float} and {@code double}
* arithmetic in Java.
*
*<p>Example:
*<table border>
* <caption><b>Rounding mode HALF_EVEN Examples</b></caption>
*<tr valign=top><th>Input Number</th>
* <th>Input rounded to one digit<br> with {@code HALF_EVEN} rounding
*<tr align=right><td>5.5</td> <td>6</td>
*<tr align=right><td>2.5</td> <td>2</td>
*<tr align=right><td>1.6</td> <td>2</td>
*<tr align=right><td>1.1</td> <td>1</td>
*<tr align=right><td>1.0</td> <td>1</td>
*<tr align=right><td>-1.0</td> <td>-1</td>
*<tr align=right><td>-1.1</td> <td>-1</td>
*<tr align=right><td>-1.6</td> <td>-2</td>
*<tr align=right><td>-2.5</td> <td>-2</td>
*<tr align=right><td>-5.5</td> <td>-6</td>
*</table>
*/
HALF_EVEN(BigDecimal.ROUND_HALF_EVEN),
/**
* Rounding mode to assert that the requested operation has an exact
* result, hence no rounding is necessary. If this rounding mode is
* specified on an operation that yields an inexact result, an
* {@code ArithmeticException} is thrown.
*<p>Example:
*<table border>
* <caption><b>Rounding mode UNNECESSARY Examples</b></caption>
*<tr valign=top><th>Input Number</th>
* <th>Input rounded to one digit<br> with {@code UNNECESSARY} rounding
*<tr align=right><td>5.5</td> <td>throw {@code ArithmeticException}</td>
*<tr align=right><td>2.5</td> <td>throw {@code ArithmeticException}</td>
*<tr align=right><td>1.6</td> <td>throw {@code ArithmeticException}</td>
*<tr align=right><td>1.1</td> <td>throw {@code ArithmeticException}</td>
*<tr align=right><td>1.0</td> <td>1</td>
*<tr align=right><td>-1.0</td> <td>-1</td>
*<tr align=right><td>-1.1</td> <td>throw {@code ArithmeticException}</td>
*<tr align=right><td>-1.6</td> <td>throw {@code ArithmeticException}</td>
*<tr align=right><td>-2.5</td> <td>throw {@code ArithmeticException}</td>
*<tr align=right><td>-5.5</td> <td>throw {@code ArithmeticException}</td>
*</table>
*/
UNNECESSARY(BigDecimal.ROUND_UNNECESSARY);
注意
占位符’#'和‘0’同时声明使用的情况下,如果整数为0,则不会使用0填充,只会为空。
整数为1的话,则正常占位显示