In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "Java's BigInteger and BigDecimal class instance analysis". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian slowly and deeply to study and learn "Java's BigInteger and BigDecimal class instance analysis" together!
BigInteger class
In Java, there are many classes that deal with numbers, such as the Integer class, but the Integer class also has an upper limit. And its maximum value is 2^31-1.
If we want to represent a larger number at this point, we use Integer is not representable, where Java provides the BigInteger class.
The BigInteger class supports infinitely large numbers and supports integers of arbitrary precision, which means that it can accurately represent arbitrary values without missing.
And I want to emphasize here that because the number type you pass in is a character type, you can't use + - * /when you're doing operations.
The corresponding is the way to use them:
add(),subtract(),multiply(),divide()
There are also some common methods:
Equals method. is compared
Code demonstration:
import java.math.BigInteger;public class Demo01 { public static void main(String[] args) { BigInteger bigInteger1 = new BigInteger("2222222222222222222222222222222"); BigInteger bigInteger2 = new BigInteger("1111111111111111111111111111111"); System.out.println("bigInteger1 large numbers are: " + bigInteger1); System.out.println("bigInteger2 large numbers are: " + bigInteger2); System.out.println("add two large numbers: " + bigInteger 1.add (bigInteger2));//add System.out.println("subtract two large numbers: " + bigInteger 1.subtract (bigInteger2));//subtract System.out.println("multiply two large numbers: " + bigIntegr1.multiply (bigInteger2));//multiply System.out.println("divide two large numbers: " + bigInteger 1.divide (bigInteger2));//divide System.out.println("compare two large numbers: " + bigIntegr1.equals (bigInteger2));//equals compare }}
When doing division, if there are decimal places, it is this to intercept the decimal places.
Of course, there are not only integer types in this respect, but also floating point types.
BigDecimal class
Of course, the precision of floating-point types can also be as large as possible.
import java.math.BigDecimal;public class Demo02 { public static void main(String[] args) { BigDecimal bigDecimal1 = new BigDecimal("12232423432432.53241234324"); BigDecimal bigDecimal2 = new BigDecimal("2.0"); System.out.println("bigDecimal1 value: " + bigDecimal1); System.out.println("bigDecimal2 value: " + bigDecimal2); System.out.println("add: " + bigDecimal1.add(bigDecimal2)); System.out.println("subtract: " + bigDecimal1.subtract(bigDecimal2)); System.out.println("multiply: " + bigDecimal1.multiply(bigDecimal2)); System.out.println(" + bigDecimal1.divide(bigDecimal2)); }}
In BigDecimal's method, it also better solves precision problems in java language (for example, the result of 0.1x3 == 0.3 is false)
The problem of division incompleteness in BigDecimal class
However, there is one caveat when using BigDecimal (it will choose error when it encounters something that cannot be eliminated).
So when doing division, just give it a truncated digit.
Let's look at one method first:
public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)
divisor -divisor.
scale -several digits after decimal point
roundingMode -Select rounding mode
So we can write it like this:
Rounding mode selection:
ROUND_CEILING Rounds to positive infinity ROUND_DOWN Rounds to negative infinity ROUND_FLOOR Rounds to negative infinity ROUND_HALF_DOWN Rounds to encounter.5 Rounds down ROUND_HALF_UP Rounds to encounter.5 Rounds up ROUND_HALF_EVEN Rounds to encounter 5 Look at the previous number, greater than 5 Up, less than 5 Down, equal to 5 Look forward again Thank you for reading, the above is the "Java BigInteger and BigDecimal class instance analysis" content, after the study of this article, I believe that we have a deeper understanding of Java BigInteger and BigDecimal class instance analysis, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
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.