Java Fundamentals Hardening the Introduction and overview of BigDecimal classes for 89:bigdecimal classes and the use of BigDecimal (subtraction)

Source: Internet
Author: User

1. BigDecimal class overview:

Since the float type and double are easy to lose precision when calculating. So in order to accurately express and calculate floating-point numbers, Java provides bigdecimal.

BigDecimal: A signed decimal number that is immutable and arbitrarily accurate.

2. BigDecimal Construction method

1  Public BigDecimal (String val)

3. use of BigDecimal (subtraction)

  1  public   BigDecimal Add (BigDecimal augend): Add  2   Public   BigDecimal subtract (BigDecimal subtrahend): minus  3  public   BigDecimal multiply ( BigDecimal divisor): Multiply  4  public  Span style= "color: #000000;" > BigDecimal divide (BigDecimal divisor): except  5  public  BigDecimal Divide (BigDecimal divisor, int  scale, int  roundingmode):  quotient, several decimals (scale), How to take (roundingmode)   

Code Demo:

1  Packagecn.itcast_02;2 3 ImportJava.math.BigDecimal;4 5 /*6 * Construction Method:7 * Public BigDecimal (String val)8  * 9 * Public BigDecimal Add (BigDecimal augend)Ten * Public BigDecimal Subtract (BigDecimal subtrahend) One * Public BigDecimal Multiply (BigDecimal multiplicand) A * Public BigDecimal Divide (BigDecimal divisor) - * Public BigDecimal Divide (BigDecimal divisor,int scale,int roundingmode): quotient, several decimals, how to take out -  */ the  Public classBigdecimaldemo { -      Public Static voidMain (string[] args) { -         //System.out.println (0.09 + 0.01); -         //System.out.println (1.0-0.32); +         //System.out.println (1.015 *); -         //System.out.println (1.301/100); +  ABigDecimal BD1 =NewBigDecimal ("0.09"); atBigDecimal Bd2 =NewBigDecimal ("0.01"); -System.out.println ("Add:" +Bd1.add (BD2)); -System.out.println ("-------------------"); -  -BigDecimal Bd3 =NewBigDecimal ("1.0"); -BigDecimal Bd4 =NewBigDecimal ("0.32"); inSystem.out.println ("Subtract:" +bd3.subtract (Bd4)); -System.out.println ("-------------------"); to  +BigDecimal Bd5 =NewBigDecimal ("1.015"); -BigDecimal Bd6 =NewBigDecimal ("100"); theSystem.out.println ("Multiply:" +bd5.multiply (Bd6)); *System.out.println ("-------------------"); $ Panax NotoginsengBigDecimal Bd7 =NewBigDecimal ("1.301"); -BigDecimal Bd8 =NewBigDecimal ("100"); theSystem.out.println ("Divide:" +bd7.divide (Bd8)); +System.out.println ("Divide:" A+ Bd7.divide (Bd8, 3, bigdecimal.round_half_up)); theSystem.out.println ("Divide:" ++ Bd7.divide (Bd8, 8, bigdecimal.round_half_up)); -     } $}

Operating effect:

Java Fundamentals Hardening the Introduction and overview of BigDecimal classes for 89:bigdecimal classes and the use of BigDecimal (subtraction)

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.