对BigDecimal源码进行一些浅层次研究
BigDecimal amount = new BigDecimal(1.0);
BigDecimal amount2 = new BigDecimal(3.0);
System.out.println(amount.divide(amount2));
/*
* If the quotient this/divisor has a terminating decimal
* expansion, the expansion can have no more than
* (a.precision() + ceil(10*b.precision)/3) digits.
* Therefore, create a MathContext object with this
* precision and do a divide with the UNNECESSARY rounding
* mode.
*/
大概意思是
/**
* 如果商(this/divisor)即是一个有限的小数,那么这个小数的位数最多不会超过
* (a.precision() + ceil(10*b.precision)/3) 个。
* 这里 a 和 b 分别代表被除数和除数,.precision() 方法返回的是它们的有效数字位数(不包括小数点)。
* 因此,在进行除法之前,可以创建一个 MathContext 对象,将精度设置为上述计算得到的最大可能位数,
* **/
BigDecimal amount = new BigDecimal("1.0");
BigDecimal amount2 = new BigDecimal("3.0");
// 指定精度为2位小数,并采用HALF_UP舍入模式
MathContext mc = new MathContext(2, RoundingMode.HALF_UP);
BigDecimal result = amount.divide(amount2, mc);
System.out.println(result);
Java BigDecimal 隐藏的第二个bug
BigDecimal amount = new BigDecimal(1.23);
System.out.println(amount);
乍一看好像会输出1.23,实则不然,每次运行我们都会获得不一样的结果,比如我某次就获得了
1.229999999999999982236431605997495353221893310546875
Translates a double into a BigDecimal which is the exact decimal representation of the double's binary floating-point value. The scale of the returned BigDecimal is the smallest value such that (10scale × val) is an integer.
Notes:
The results of this constructor can be somewhat unpredictable. One might assume that writing new BigDecimal(0.1) in Java creates a BigDecimal which is exactly equal to 0.1 (an unscaled value of 1, with a scale of 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the value that is being passed in to the constructor is not exactly equal to 0.1, appearances notwithstanding.
The String constructor, on the other hand, is perfectly predictable: writing new BigDecimal("0.1") creates a BigDecimal which is exactly equal to 0.1, as one would expect. Therefore, it is generally recommended that the String constructor be used in preference to this one.
When a double must be used as a source for a BigDecimal, note that this constructor provides an exact conversion; it does not give the same result as converting the double to a String using the Double.toString(double) method and then using the BigDecimal(String) constructor. To get that result, use the static valueOf(double) method.
Params:
val – double value to be converted to BigDecimal.
Throws:
NumberFormatException – if val is infinite or NaN.
valueOf(double)底层原理如何
public static BigDecimal valueOf(double val) {
// Reminder: a zero double returns '0.0', so we cannot fastpath
// to use the constant ZERO. This might be important enough to
// justify a factory approach, a cache, or a few private
// constants, later.
return new BigDecimal(Double.toString(val));
}
其实他的底层原理就是BigDecimal(String)
点个关注吧
推荐站内搜索:最好用的开发软件、免费开源系统、渗透测试工具云盘下载、最新渗透测试资料、最新黑客工具下载……
还没有评论,来说两句吧...