[Switch] some experiences in sorting out Java large numbersBiginteger and bigdecimal databases. They are big integers and big real numbers (the specific size is unknown ).Operation:1. include: Import java. Math .*;2. Application:Single element: biginteger a, B; bigdecimal A, B;Array:Biginteger A [];A = new biginteger [1024];Bigdecimal A [];A = new bigdecimal [1024];3. assignment:A [1] = bigdecimal. valueof (1 );A [1] = biginteger. valueof (1 );4. addition, subtraction, multiplication, division:Biginteger A, B;A = biginteger. valueof (1 );B = biginteger. valueof (2 );System. Out. println (A. Add (B ));System. Out. println (A. Subtract (B ));System. Out. println (A. Multiply (B ));System. Out. println (A. Divide (B ));Bigdecimal A, B;A = bigdecimal. valueof (5 );B = bigdecimal. valueof (3 );System. Out. println (A. Add (B ));System. Out. println (A. Subtract (B ));System. Out. println (A. Multiply (B ));System. Out. println (A. Divide (bigdecimal. valueof (1 )));// Division is required here, otherwise reSystem. Out. println (A. Divide (B, 4, 4 ));// What is the meaning of the 3rd parameters?System. Out. println (A. Divide (B, 4, bigdecimal. round_half_up ));// Here, the second parameter is the reserved digits, and the third parameter is the carry mode.5. Compare size:Bigdecimal A, B;A = bigdecimal. valueof (5 );B = bigdecimal. valueof (3 );If (A. compareto (B)> 0)System. Out. println ("A is greater than B ");Else if (A. compareto (B) = 0)System. Out. println ("A is equal to B ");Else if (A. compareto (B) <0)System. Out. println ("A is lower than B ");
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service