Java large number Class introduction

Source: Internet
Author: User
Tags array definition

Java can handle large numbers of classes with two high-precision large integersBigIntegerand high-precision floating-point numbersbigdecimal, These two classes are in the Java.math package, and to use them you must refer to the package before the class: import Java.math. biginteger; and import Java.math. bigdecimal; or import java.math.*;

The following from several aspects to the biginteger and bigdecima To do a simple comparison:

A. constant

BigInteger: One,zero,ten represents 1,0,10 respectively.

The definition is similar to the following: public static final BigInteger one = valueOf (1);

BigDecimal: There are 8 constants for rounding in addition to the above three constants:round_up,round_down,round_ceiling, Round_floor,round_half_up,

Round_half_down, Round_half_even, round_unnecessary. For detailed rounding patterns, consult the Java API.

Two. Declaration Assignment

BigInteger: BigInteger bi = new BigInteger(byte[] val) ;

New BigInteger(int signum, byte[] magnitude) ;

New BigInteger(int bitLength, int certainty,Random rnd) .

New BigInteger(int numBits,Random rnd) .

New BigInteger(String val) .

new BigInteger(String val, int radix)

Constructors can only accept these types,for example, the definition is wrong: BigInteger bi = new BigInteger;

Or: BigInteger bi = biginteger.valueof (100);

The array definition is similar to the base type.

BigDecimal: BigDecimal bd = new BigDecimal (100) OR: BigDecimal BD = bigdecimal.valueof (100);

BigDecimal has more constructors than BigInteger and feels easier to use.

By the way, the scanner class in the Java.util package implements the Nextbiginteger () and Nextbigdecimal () methods, which can be used to read the BigInteger and bigdecimal of the console input. Give a sample example:

Three. Related functions

Add (), subtract (), pow (), ABS (),multiply()等等这一类就不介绍了,奇妙的是probablePrime(int bitLength, Random rnd),  nextProbablePrime()这一类竟然和素数扯得上关系。

BigDecimal a few more ways of formatting control, which is very useful for processing output in a variety of different formats.

Striptraillingzeros (): Remove all 0 without affecting the size of the value.

1.50->1.5;

1.00->1;

This function is very practical.

Everyone knows that Java classes generally have to take the ToString method. BigDecimal There are three methods of tostring,toplainstring and toengineeringstring that are represented as strings.

Here are the features of these three methods:

ToString: using scientific notation If an exponent is needed;

Toengineeringstring:using engineering notation If an exponent is needed.

Toplainstring:without an exponent field.







Java large number Class introduction

Related Article

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.