JAVA Big Data Processing (BigInteger, BigDecimal)

Source: Internet
Author: User

It is very troublesome to process large numbers in C or C ++, but there are two classes in JAVA: BigInteger and BigDecimal, which respectively represent the big integer class and the big floating point class, objects of the two classes can indicate the maximum range is unclear. Theoretically, they can represent a large number of wireless devices, as long as the computer memory is large enough.

Both classes are in the java. math. * package, so the package must be referenced at the beginning each time.

I basic functions:

1. valueOf (parament); convert the parameter to a specified type

For example, int a = 3;

BigInteger B = BigInteger. valueOf ();

Then B = 3;

String s = "12345 ";

BigInteger c = BigInteger. valueOf (s );

C = 12345;

2. add (); add large Integers

BigInteger a = new BigInteger ("23 ");

BigInteger B = new BigInteger ("34 ");

A. add (B );

3. subtract (); Subtraction

4. multiply (); multiply

5. divide (); Division and rounding

6. remainder (); obtain the remainder

7. pow (); a. pow (B) = a ^ B

8. gcd (); maximum common approx.

9. abs (); absolute value

10. negate (); returns the inverse number.

11. mod (); a. mod (B) = a % B = a. remainder (B );

12. max (); min ();

13. punlic int comareTo ();

14. boolean equals (); Equal

15. BigInteger constructor:

The following two types are generally used:

BigInteger (String val );

Converts a specified string to a decimal representation;

BigInteger (String val, int radix );

Converts the string representation of the BigInteger of the specified base to BigInteger.

Ii. Basic constants:

A = BigInteger. ONE 1

B = BigInteger. TEN 10

C = BigInteger. ZERO 0

III. Basic operations

1. Read:

Use the consumer class to define objects for console reading. The consumer class is in the java. util. * package.

Cin = new partition (System. in); // read

While (cin. hasNext () // equivalent! = EOF

{

Int n;

BigInteger m;

N = cin. nextInt (); // read an int;

M = cin. BigInteger (); // read a BigInteger;

System. out. print (m. toString ());

}

Iv. Application

Four budgets:

Import java. util. collections;
Import java. math .*;
Import java. text .*;

Public class Main
{
Public static void main (String args [])
{
Cin = new partition (System. in );
BigInteger a, B;
Int c;
Char op;
String s;
 
While (cin. hasNext ())
{
A = cin. nextBigInteger ();
S = cin. next ();
Op = s. charAt (0 );
If (op = '+ ')
{
B = cin. nextBigInteger ();
System. out. println (a. add (B ));
}
Else if (op = '-')
{
B = cin. nextBigInteger ();
System. out. println (a. subtract (B ));
}
Else if (op = '*')
{
B = cin. nextBigInteger ();
System. out. println (a. multiply (B ));
}
Else
{
BigDecimal a1, b1, eps;
String s1, s2, temp;
S1 = a. toString ();
A1 = new BigDecimal (s1 );
B = cin. nextBigInteger ();
S2 = B. toString ();
B1 = new BigDecimal (s2 );
C = cin. nextInt ();
Eps = a1.divide (b1, c, 4 );
// System. out. println (a + "" + B + "" + c );
// System. out. println (a1.doubleValue () + "" + b1.doubleValue () + "" + c );
System. out. print (a. divide (B) + "" + a. mod (B) + "");
If (c! = 0)
{
Temp = "0 .";
For (int I = 0; I <c; I ++) temp + = "0 ";
DecimalFormat gd = new DecimalFormat (temp );
System. out. println (gd. format (eps ));
}
Else System. out. println (eps );
}
}
}
}

Supplement:

A = a. pow (B );
A = a. stripTrailingZeros ();
D = a. toPlainString ();
If (d. charAt (0) = '0') d = d. substring (1 );

Author: "Jelly_JAVA blog"
 

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.