Java Large Value

Source: Internet
Author: User

A string of 20 digits is required to generate ICCID

You want to use Excel's Auto-add feature but always show the scientific notation, which cannot be achieved ...

At last

ImportJava.math.BigDecimal; Public classNumberincrease { Public Static voidMain (string[] args) {//TODO auto-generated Method StubBigDecimal k =NewBigDecimal ("89860095000000000019");  for(Longi=0;i<100;i++) {k=k.add (NewBigDecimal ("1"));        System.out.println (K.tostring ()); }                    }}

89860095000000000020
89860095000000000021
89860095000000000022
89860095000000000023
89860095000000000024
89860095000000000025
89860095000000000026
89860095000000000027
89860095000000000028
89860095000000000029
89860095000000000030
89860095000000000031
89860095000000000032
89860095000000000033
89860095000000000034
89860095000000000035
89860095000000000036
89860095000000000037
89860095000000000038
89860095000000000039
89860095000000000040
89860095000000000041
89860095000000000042
89860095000000000043
89860095000000000044
89860095000000000045

······

The following is reproduced

If you think long and double cannot meet your requirements, Java also provides two classes BigInteger and BigDecimalthat represent large values. They provide arbitrary precision, and to perform mathematical operations on them, they need to invoke the methods they provide instead of the operators, subtraction are the add, subtract, divide, and multiply methods, respectively.

The use of BigInteger is relatively simple, there is nothing to say, so I will not waste my breath.

The BigDecimal is made up of BigInteger and scales (scale) , which determines the position of the decimal point. So, we can construct 1.234 in the following way

For the addition, subtraction and multiplication between BigDecimal, we can not consider the scale, after all, no matter how the calculation, the number of decimal place is limited. But the division is different, think about 1/3 what is the result? Try running the following code.

BigDecimal B1 = new BigDecimal ("1.0"); BigDecimal b2 = new BigDecimal ("3.0"); System.out.println (B1.divide (B2));


The above code throws an ArithmeticException exception and should be written if you want to keep two decimal places.

BigDecimal B1 = new BigDecimal ("1.0"); BigDecimal b2 = new BigDecimal ("3.0"); System.out.println (B1.divide (B2, 2, roundingmode.half_up));

The following two concepts must be understood in order to perform operations on BigDecimal.

Precision (precision) refers to the number of digits of all numbers.

The Rounding Method (rounding mode) is relatively well understood, and BigDecimal supports the following rounding methods.

Digital Up Down CEILING Floor Half_up Half_down Half_even
5.5 6 5 6 5 6 5 6
2.5 3 2 3 2 3 2 2
1.6 2 1 2 1 2 2 2
1.1 2 1 2 1 1 1 1
1 1 1 1 1 1 1 1
-1 -1 -1 -1 -1 -1 -1 -1
-1.1 -2 -1 -1 -2 -1 -1 -1
-1.6 -2 -1 -1 -2 -2 -2 -2
-2.5 -3 -2 -2 -3 -3 -2 -2
-5.5 -6 -5 -5 -6 -6 -5 -6

Java uses the Mathcontext class to encapsulate these two concepts and take a look at the following example.

public static void Main (string[] args) throws Exception {    BigDecimal B1 = new BigDecimal (New BigInteger ("1234"), 3); /1.234     BigDecimal b2 = new BigDecimal (New BigInteger ("Ten"), 1);//1.0        BigDecimal add = B1.add (b2). Setscale (2, Ro UNDINGMODE.HALF_UP); Keep 2 decimal    System.out.println ("1.234 + 1.0 =" + add);        BigDecimal subtract = b1.subtract (B2). Setscale (2, roundingmode.half_up); Keep 2 decimal    System.out.println ("1.234-1.0 =" + subtract);        BigDecimal multiply = b1.multiply (b2). Setscale (2, roundingmode.half_up); Keep 2 decimal    System.out.println ("1.234 * 1.0 =" + multiply);        BigDecimal divide = B1.divide (B2, 2, roundingmode.half_up); Keep 2 decimal    System.out.println ("1.234/1.0 =" + divide);

Java Large Value

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.