Application of the biginteger and bigdecimal classes in Java

Source: Internet
Author: User

The two classes of Java, biginteger and bigdecimal, represent the big integer class and the big floating point class respectively. Theoretically, they can represent an infinite number.


Biginteger:

Package COM. xujin; import Java. util. *; import Java. math. *; public class test {public static void main (string [] ARGs) {using CIN = new using (system. in); // biginteger type constant biginteger A = biginteger. one; system. out. println ("biginteger. the result of one is "+ a); // 1 biginteger B = biginteger. ten; system. out. println ("biginteger. the result of ten is "+ B); // 10 biginteger c = biginteger. zero; system. out. println ("biginteger. zero returns" + C); // 0 // initialize biginteger c = new biginteger ("12345670", 8); // C = 01234567890, system. out. println (c); // 2739128 biginteger d = biginteger. valueof (100); // d = 100 biginteger E = new biginteger (New byte [] {00000001}); // bytes exceed system. out. println (E); // 256system. out. println (E. bitcount (); system. out. println (E. bitlength (); // calculate system. out. println ("enter a big integer A, B"); While (CIN. hasnext () {// equivalent! = Eofbiginteger A = cin. nextbiginteger (); biginteger B = cin. nextbiginteger (); biginteger C1 =. add (B); // Large Number Addition system. out. println ("the added result is" + C1); biginteger C2 =. subtract (B); // subtraction of large numbers system. out. println ("the result of subtraction is" + C2); biginteger C3 =. multiply (B); // multiplication of large numbers system. out. println ("the multiplication result is" + C3); biginteger C4 =. divide (B); // division of large numbers by system. out. println ("the result of division is" + C4); biginteger C5 =. moD (B); system. out. println ("the result of the modulo is" + C5); biginteger cc5 =. remainder (B); system. out. println ("the remainder result is" + cc5); biginteger C6 =. max (B); // obtain the largest system. out. println ("Max" + C6); biginteger C7 =. min (B); // obtain the minimum system. out. println ("minimum" + C7); biginteger C8 =. pow (10); // exponential calculation system. out. println ("exponential calculation result:" + C8); if (. equals (B) // determines whether the system is equal. out. println ("Equal"); elsesystem. out. println ("not equal"); biginteger C10 =. ABS (); // calculates the absolute value system. out. println ("the absolute value of A is" + C10); biginteger C11 =. negate (); // returns the opposite number system. out. println ("the opposite number of A is" + C11 );}}}


Bigdecimal:

// Create a bigdecimal object

Bigdecimal bignumber = new bigdecimal ("89.1234567890123456789 ");

Bigdecimal bigrate = new bigdecimal (1000 );

Bigdecimal bigresult = new bigdecimal (); // The value of the bigresult object is 0.0.

Note that the divide function in bigdecimal is slightly different from that in biginteger.

Method 1:
Pubilc bigdecimal divide (bigdecimal divisor)

API explanation: return a bigdecimal with the value (This/divisor), and the preferred scale is (this. scale ()-divisor. scale (); arithmeticexception is thrown if the exact business value cannot be expressed (because it has infinite decimal extensions.

Method 2:
Pubilc bigdecimal divide (bigdecimal divisor, int scale, int roundingmode)

Scale refers to the number of digits after the decimal point. For example, if it is 123.456, scale is 3, which is a method in the bigdecimal class. For example, bigdecimal B = new bigdecimal ("123.456"); // B. Scale (), 3 is returned.
Roundingmode is the retention mode of decimal places. They are all constant fields in bigdecimal.

For example, bigdecimal. round_half_up indicates 4 to 5.

Pubilc bigdecimal divide (bigdecimal divisor, int scale, int roundingmode)
The result of dividing a bigdecimal object by the divisor is required to retain the result with a scale. roundingmode indicates the retention mode, whether it is rounded down or not, you can select one by yourself!


Method 3:
Pubilc bigdecimal divide (bigdecimal divisor, int scale, roundingmode)

Fields
Final public static bigdecimal Zero
Zh_cn

The value is 0, and the scale is 0.

Since 1.5
Final public static bigdecimal One
Zh_cn

The value is 1 and the scale is 0.

Since 1.5
Final public static bigdecimal Ten
Zh_cn

The value is 10, and the scale is 0.

Since 1.5
Final public static int Round_up
Zh_cn

Rounding mode away from zero. Always add a number before discarding the non-zero part. Note that this round-robin mode never reduces the size of the calculated value.

Final public static int Round_down
Zh_cn

Rounding mode close to zero. No number (namely, truncated) is added before a part is discarded ). Note that this round-robin mode never increases the size of the calculated value.

Final public static int Round_ceiling
Zh_cn

Rounding mode close to positive infinity. IfBigdecimalIf it is positive, the rounding behavior matchesRound_upSame; if it is negative, the rounding behavior is the sameRound_downSame. Note that this round-robin mode never reduces the calculated value.

Final public static int Round_floor
Zh_cn

Rounding mode close to negative infinity. IfBigdecimalIf it is positive, the rounding behavior matchesRound_downSame; if it is negative, the rounding behavior is the sameRound_upSame. Note that this round-robin mode will never increase the calculated value.

Final public static int Round_half_up
Zh_cn

Round to the nearest number. If the distance between the two adjacent numbers is equal, the round-up mode is used. If the discard part is greater than or equal to 0.5, the rounding behavior matchesRound_upSame; otherwise, the rounding behavior is the sameRound_downSame. Note that this is the rounding mode most of us learned in elementary school.

Final public static int Round_half_down
Zh_cn

Round to the nearest number. If the distance between the two adjacent numbers is equal, the rounded up mode is used. If you discard part> 0.5, the rounding behavior matchesRound_upSame; otherwise, the rounding behavior is the sameRound_downSame.

Final public static int Round_half_even
Zh_cn

Round to the nearest number. If the distance between two adjacent numbers is equal, round to the adjacent even number. If the number on the left of the discard part is an odd number, the rounding behavior matchesRound_half_upSame; if it is an even number, the rounding behavior is the sameRound_half_downSame. Note that this round-robin mode can minimize the number of accumulated errors when a series of computations are repeated.

Final public static int Round_unnecessary
Zh_cn

The Operations requested by assertion have precise results and therefore do not require rounding. If this round-robin mode is specified for an operation that obtains the exact resultArithmeticexception.

The function is the same as the big integer above:

Package COM. xujin; import Java. util. *; import Java. math. *; public class test {public static void main (string [] ARGs) {using CIN = new using (system. in); // bigdecimal type constant bigdecimal A = bigdecimal. one; system. out. println ("bigdecimal. the result of one is "+ a); // 1 bigdecimal B = bigdecimal. ten; system. out. println ("bigdecimal. the result of ten is "+ B); // 10 bigdecimal c = bigdecimal. zero; system. out. println ("bigdecimal. zero returns" + C); // 0 // initialize bigdecimal c = new bigdecimal ("89.1234567890123456789"); bigdecimal d = new bigdecimal (100 ); bigdecimal E = new bigdecimal (New char [] {'2', '1 ','. ', '2'}); system. out. println (E); // 21.2 // calculate system. out. println ("enter a big integer A, B"); While (CIN. hasnext () {// equivalent! = Eofbigdecimal A = cin. nextbigdecimal (); bigdecimal B = cin. nextbigdecimal (); bigdecimal C1 =. add (B); // Large Number Addition system. out. println ("the added result is" + C1); bigdecimal C2 =. subtract (B); // subtraction of large numbers system. out. println ("the result of subtraction is" + C2); bigdecimal C3 =. multiply (B); // multiplication of large numbers system. out. println ("the multiplication result is" + C3); // Note: if the Division is not completed, an arithmeticexception error bigdecimal C4 = A will be thrown. divide (B); // division of large numbers by system. out. println ("the result of division is" + C4); bigdecimal cc5 =. remainder (B); system. out. println ("the remainder result is" + cc5); bigdecimal C6 =. max (B); // obtain the largest system. out. println ("Max" + C6); bigdecimal C7 =. min (B); // obtain the minimum system. out. println ("minimum" + C7); bigdecimal C8 =. pow (10); // exponential calculation system. out. println ("exponential calculation result:" + C8); if (. equals (B) // determines whether the system is equal. out. println ("Equal"); elsesystem. out. println ("not equal"); bigdecimal C10 =. ABS (); // calculates the absolute value system. out. println ("the absolute value of A is" + C10); bigdecimal C11 =. negate (); // returns the opposite number system. out. println ("the opposite number of A is" + C11 );}}}

Practical format conversion:

// Remove the suffix 0 bigdecimal BD = new bigdecimal ("12000.87300"); BD = BD. striptrailingzeros (); system. out. println (BD); BD = new bigdecimal ("1.2e-3"); // BD = new bigdecimal ("1.2e + 3"); // descientific notation if (BD. scale () <0) {BD = BD. setscale (0);} system. out. println (BD); // retain N decimal places. n = 5: BD = new bigdecimal ("12000.873000"); BD = BD. setscale (5, bigdecimal. round_half_up); system. out. println (BD );

References:
Java API http://doc.java.sun.com/DocWeb/api/java.math.BigDecimal

Http://blog.163.com/wangyongfei_2008@yeah/blog/static/1722383292011535174177/

Http://qingfengxia2.blog.163.com/blog/static/25478407201012442119977/

Http://www.cnblogs.com/ffjjqqjj/archive/2011/07/14/2105893.html, etc.

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.