Java's BigDecimal class

Source: Internet
Author: User

1  Public classDemo5_bigdecimal {2 3     /*4 * A:bigdecimal Overview5 * float and double are easy to lose precision, so in order to accurately represent and calculate floating-point numbers, Java provides bigdecimal6 * Immutable, arbitrary-precision signed decimal number. 7 * B: Construction method8 * Public BigDecimal (String val)9 * C: Member MethodTen * Public BigDecimal Add (BigDecimal augend) One * Public BigDecimal Subtract (BigDecimal subtrahend) A * Public BigDecimal Multiply (BigDecimal multiplicand) - * Public BigDecimal Divide (BigDecimal divisor) -      */ the      Public Static voidMain (string[] args) { -System.out.println (2.0-1.1);//0.8999999999999999 can be infinitely close to 0.9 -          -BigDecimal B11 =NewBigDecimal (2.0); +BigDecimal B22 =NewBigDecimal (1.1); -System.out.println (B11.subtract (B22));//The result is a slightly more precise 0.899999999999999911182158029987476766109466552734375 +          ABigDecimal B1 =NewBigDecimal ("2.0"); atBigDecimal B2 =NewBigDecimal ("1.1"); -System.out.println (B1.subtract (B2));//by constructing parameters passed into the string, the result is an exact 0.9 -          -BigDecimal BD1 = bigdecimal.valueof (2.0); -BigDecimal Bd2 = bigdecimal.valueof (1.1); -System.out.println (Bd1.subtract (BD2));//this way you can also get the result for the exact 0.9 in  -     } to  +}

Java's BigDecimal class

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.