In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to solve the BigDecimal pit in Java. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Java BigDecimal pit BigDecimal bd = new BigDecimal (0.1); System.out.println ("result:" + bd); result: 158.7400000000009094947017729282379150390625
1) the result of the construction method whose parameter type is double has some unpredictability. One might think that the BigDecimal created by writing newBigDecimal (0.1) in Java is exactly equal to 0.1 (a non-scalar value of 1 with a scale of 1), but it is actually equal to 0.10000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be accurately represented as double (or, in this case, as any finite length binary decimal). In this way, the value passed into the constructor is not exactly equal to 0.1 (although ostensibly equal to that value).
2) the String constructor is completely predictable: writing newBigDecimal ("0.1") creates a BigDecimal that is exactly equal to the expected 0.1. Therefore, by comparison, it is generally recommended that the String construction method be preferred.
3) when double must be used as the source of BigDecimal, note that this constructor provides an exact conversion; it does not provide the same result as using the Double.toString (double) method, and then using the BigDecimal (String) constructor to convert double to String. To get this result, use the static valueOf (double) method.
Solution method
I.
BigDecimal loanAmount = new BigDecimal ("15000.48"); System.out.println ("unconverted format:" + loanAmount); result: unconverted format: 15000.48
II.
NumberFormat currency = NumberFormat.getCurrencyInstance (); / / establish currency format reference NumberFormat percent = NumberFormat.getPercentInstance (); / establish percentage format reference percent.setMaximumFractionDigits (3); / / percentage decimal point up to 3 digits BigDecimal loanAmount = new BigDecimal ("15000.48"); / / loan amount System.out.println ("unconverted format:" + loanAmount); BigDecimal interestRate = new BigDecimal ("0.008") / / interest rate BigDecimal interest = loanAmount.multiply (interestRate); / / multiplicative System.out.println ("loan amount:\ t" + currency.format (loanAmount)); System.out.println ("interest rate:\ t" + percent.format (interestRate)); System.out.println ("interest:\ t" + currency.format (interest)); BigDecimal bd = new BigDecimal (158.74); System.out.println ("result:" + bd); BigDecimal-prone pits
BigDecimal is generally used to do more demanding accurate calculations. I encountered a big pit when I was using it a few days ago. Record it.
The problem arises from using BigDecimal to do division (divide) operations, and there are three commonly used constructors for the divide method of this class.
BigDecimaldivide (BigDecimal divisor) Returns a BigDecimal whose value is (this / divisor), and whose preferred scale is (this.scale ()-divisor.scale ()); if the exact quotient cannot be represented (because it has a non-terminating decimal expansion) an ArithmeticException is thrown.BigDecimaldivide (BigDecimal divisor, int roundingMode) Returns a BigDecimal whose value is (this / divisor), and whose scale is this.scale (). BigDecimaldivide (BigDecimal divisor, int scale, int roundingMode) Returns a BigDecimal whose value is (this / divisor), and whose scale is as specified.
In the first constructor, you can pass in only the divisor (divisor).
However, it is extremely easy to make an error when calling this method. When the division is not complete, an infinite loop decimal will be generated, and an exception will be thrown:
Java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
At java.math.BigDecimal.divide (BigDecimal.java:1690)
In the second or third constructor, it is specified that when the division is inexhaustible, the significant digits are reserved, so it is safer.
It is recommended that you use the third method when using this method, which is safer.
This is the end of the article on "how to solve the pit of BigDecimal in Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.